- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <string.h>
- int main(int argc,char **argv)
- {
- int sockfd;
- int n;
- struct sockaddr_in servaddr,cliaddr;
- char sendbuf[1000];
- char recvbuf[1000];
- if(argc != 2)
- {
- printf("usage:client <ip address>\n");
- exit(-1);
- }
- sockfd = socket(AF_INET,SOCK_STREAM,0);
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = inet_addr(argv[1]);
- servaddr.sin_port = htons(3200);
- connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
- while(fgets(sendbuf,1000,stdin) != NULL)
- {
- sendto(sockfd,sendbuf,strlen(sendbuf),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
- n = recvfrom(sockfd,recvbuf,1000,0,NULL,NULL);//(struct sockaddr*)&cliaddr,
- recvbuf[n] = '\0';
- fputs(recvbuf,stdout);
- }
- close(sockfd);
- return 0;
- }
TCP_client
最新推荐文章于 2020-05-26 16:13:21 发布