//// Created by root on 2/2/19.//#ifndef SOCKETTOY_SERVER_H#define SOCKETTOY_SERVER_H#define SERVER_IP "172.16.2.124"#define SERVER_PORT 5900#endif //SOCKETTOY_SERVER_H
//// Created by root on 2/2/19.//#include<stdio.h>#include<arpa/inet.h>#include<sys/socket.h>#include<stdlib.h>#include<unistd.h>#include<memory.h>#include"server.h"voidhandle_error(char*str){printf("Client %s error!\n",str);exit(1);}intmain(void){intre;charstr_buf[BUFSIZ]={"sdsaf"};/* create socket */intclient_socket=socket(AF_INET,SOCK_STREAM,0);if(client_socket==-1){handle_error("create socket");}/* connect */structsockaddr_inserver_addr;structin_addrbuf;inet_pton(AF_INET,SERVER_IP,&buf);server_addr.sin_family=AF_INET;server_addr.sin_port=htons(SERVER_PORT);// server_addr.sin_addr.s_addr = buf.s_addr;server_addr.sin_addr.s_addr=inet_addr(SERVER_IP);re=connect(client_socket,(structsockaddr*)&server_addr,sizeof(server_addr));if(re==-1){handle_error("connect");}printf("Input [exit] to exit the socket\n");while(1){printf("input your word: ");scanf("%s",str_buf);if(memcmp(str_buf,"exit",4)==0){break;}send(client_socket,str_buf,sizeof(str_buf),0);recv(client_socket,str_buf,sizeof(str_buf),0);printf("str_buf convert: %s\n",str_buf);}/* close */ssize_tr=send(client_socket,"exit",4,0);if(r==-1){handle_error("send");}close(client_socket);return0;}