-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtcpclientc.c
More file actions
71 lines (61 loc) · 1.4 KB
/
Copy pathtcpclientc.c
File metadata and controls
71 lines (61 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
int s;
struct sockaddr_in sin;
struct hostent *phent;
struct in_addr addr;
int ret;
char buf[100];
int port = 12345;
char addrstr[100];
char host[1000] = "localhost";
in_addr_t addrt;
if(argc>=2){
strcpy(host, argv[1]);
}
if(argc>=3){
port = atoi(argv[2]);
}
phent = gethostbyname(host);
if(!phent){
perror("gethostbyname:");
strcpy(addrstr, host);
}else{
memcpy(&addr, phent->h_addr, phent->h_length);
strcpy(addrstr, inet_ntoa(addr));
}
s = socket(PF_INET, SOCK_STREAM, 0);
if(s==-1){perror("socket:");exit(1);}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = ntohs(port);
addrt = inet_addr(addrstr);
memcpy(&sin.sin_addr,&addrt,4);
ret = connect(s, &sin, sizeof(sin));
if(ret==-1){perror("connect:");exit(1);}
{
struct linger l;
int ret;
l.l_onoff = 0;
l.l_linger = 0;
ret = setsockopt(s,SOL_SOCKET,SO_LINGER,&l,sizeof(l));
if(ret==-1){perror("setsockoet(SO_LINGER):");exit(1);}
}
while(fgets(buf, sizeof(buf), stdin)){
int ret;
printf("write begin\n");
ret = write(s, buf, strlen(buf)+1);
printf("write end ret=[%d]\n", ret);
ret = read(s, buf, sizeof(buf));
printf("readed ret=[%d]\n", ret);
printf("buf=[%s]\n", buf);
}
return 0;
}