本文给出一个很实用的服务端和客户端进行TCP通信的小例子。具体实现上非常简单,只是平时编写类似程序,具体步骤经常忘记,还要总是查,暂且将其记下来,方便以后参考。
(1)客户端程序,编写一个文件client.c,内容如下:
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> /* netdb is necessary for struct hostent */ #define PORT 4321 /* server port */ #define MAXDATASIZE 100 int main(int argc, char *argv[]) { int sockfd, num; /* files descriptors */ char buf[MAXDATASIZE]; /* buf will store received text */ struct hostent *he; /* structure that will get information about remote host */ struct sockaddr_in server; if (argc != 2) { printf("Usage: %s <IP Address> ",argv[0]); exit(1); } if((he=gethostbyname(argv[1]))==NULL) { printf("gethostbyname() error "); exit(1); } if((sockfd=socket(AF_INET,SOCK_STREAM, 0))==-1) { printf("socket() error "); exit(1); } bzero(&server,sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr = *((struct in_addr *)he->h_addr); if(connect(sockfd, (struct sockaddr *)&server, sizeof(server))==-1) { printf("connect() error "); exit(1); }
char str[] = "horst "
if((num=send(sockfd,str,sizeof(str),0))==-1){ printf("send() error "); exit(1); } if((num=recv(sockfd,buf,MAXDATASIZE,0))==-1) { printf("recv() error "); exit(1); } buf[num-1]='