-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
executable file
·54 lines (50 loc) · 1.23 KB
/
client.c
File metadata and controls
executable file
·54 lines (50 loc) · 1.23 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
//#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define IP_ADDR "192.168.1.43"
int client(unsigned short port,const char *ipaddr)
{
int sockfd;
sockfd = socket(AF_INET,SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket create faild\n");
exit(-1);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ipaddr);
if(connect(sockfd,(const struct sockaddr*)&addr,sizeof(addr)) != 0) {
perror("connect error\n");
exit(-1);
}
char buf[20] = {"it is test"};
int ret;
while(1) {
fgets(buf,20,stdin);
buf[strlen(buf) - 1] = '\0';
printf("send:%s\n",buf);
send(sockfd, buf, strlen(buf),0);
memset(buf,0 , 20);
ret = recv(sockfd,buf,20,0);
if(ret > 0){
printf("recv:%s\n",buf);
}
}
return 0;
}
int main(int argc, char **argv)
{
unsigned short port = 8888;
if(argc == 3) {
port = atoi(argv[2]);
client(port,argv[1]);
} else {
printf("input: <IP> <port>\n");
}
}