/************************
* client
************************/
#include <stdio.h> #include <errno.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h>
#define SERV_ADDR "127.0.0.1" #define SERV_PORT 5358 #define BUF_LEN 1024
void str_cli(FILE *fp, int sockfd) { char sendbuf[BUF_LEN] = {0}; char recvbuf[BUF_LEN] = {0};
while(1) { bzero(sendbuf, BUF_LEN); bzero(recvbuf, BUF_LEN); scanf("%s", sendbuf); write(sockfd, sendbuf, strlen(sendbuf)); read(sockfd, recvbuf, strlen(sendbuf)); printf("recieve: %s\n", recvbuf); } return; }
int main(int argc, char **argv) { int fd; struct sockaddr_in servaddr;
fd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr(SERV_ADDR); servaddr.sin_port = htons(SERV_PORT);
if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { printf("connect error: %s\n", strerror(errno)); return 0; }
str_cli(stdin, fd); return 0; }
|