TA的每日心情 | 慵懒 前天 10:54 |
---|
签到天数: 186 天 连续签到: 2 天 [LV.7]常住居民III
|
本帖最后由 stm1024 于 2023-12-7 09:41 编辑
上次测试了UCP,自然的,TCP的测试也安排上。
1. 编程规划
本次需要分别实现一个TCP服务端的程序,以及一个TCP客户端的程序。两次程序都在ELF-1上运行,当运行服务端程序时,在Windows中运行网络调试助手,将其作为客户端程序;当运行客户端程序时,在Windows中运行网络调试助手,将其作为服务端程序。
2. 编写程序
先写个服务端程序,运行在ELF-1开发板上:- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- void dispErrInfo(const char* pretext,int errcode)
- {
- printf("%s, errno=%d: %s\n",pretext,errcode,strerror(errcode));
- }
- char msg[256]="hello, client.";
- char rxbuf[256];
- int main()
- {
- int sktfd,ret,newsktfd;
- struct sockaddr_in saddr;
- struct sockaddr_in caddr;
- int caddr_size,nbytes;
- sktfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(sktfd<0)
- {
- dispErrInfo("create socket failed, ",errno);
- return errno;
- }
- printf("create socket with fd=%d.\n",sktfd);
- memset(&saddr,0,sizeof(saddr));
- saddr.sin_family=AF_INET;
- saddr.sin_addr.s_addr=htonl(INADDR_ANY);
- saddr.sin_port=htons(12345);
- ret=bind(sktfd,(struct sockaddr*)(&saddr),sizeof(saddr));
- if(ret<0)
- {
- dispErrInfo("bind failed",errno);
- return errno;
- }
- ret=listen(sktfd,5);
- if(ret<0)
- {
- dispErrInfo("listen failed",errno);
- return errno;
- }
- printf("Listening...\n");
- caddr_size=sizeof(caddr);
- newsktfd=accept(sktfd, (struct sockaddr*)(&caddr),(socklen_t*)(&caddr_size));
- if(newsktfd<0)
- {
- dispErrInfo("accept failed",errno);
- return errno;
- }
- //print IP & port of connected client
- //**********************************
- int n=caddr.sin_addr.s_addr;
- printf("Client IP:");
- while(n)
- {
- printf("%d",n%256);
- n/=256;
- if(n!=0)
- printf(".");
- }
- printf(" Port=%d\n",caddr.sin_port);
- //***********************************
- //send message to client
- while(1)
- {
- memset(rxbuf,0x00,256);
- nbytes=read(newsktfd,rxbuf,256);
- if(nbytes>0)
- {
- printf("Received from Client:\t%s\n",rxbuf);
- write(newsktfd, msg, sizeof(msg));
- if(strcmp(rxbuf,"exit")==0)
- break;
- }
- }
- close(newsktfd);
- close(sktfd);
- return 0;
- }
复制代码 测试效果如下:
客户端程序:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- void dispErrInfo(const char* pretext,int errcode)
- {
- printf("%s, errno=%d: %s\n",pretext,errcode,strerror(errcode));
- }
- int main()
- {
- int sktfd,ret,x=0;
- struct sockaddr_in saddr;
- char txbuf[256];
- sktfd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(sktfd<0)
- {
- dispErrInfo("create socket failed, ",errno);
- return errno;
- }
- printf("create socket with fd=%d.\n",sktfd);
- memset(&saddr,0,sizeof(saddr));
- saddr.sin_family=AF_INET;
- saddr.sin_addr.s_addr=inet_addr("192.168.1.102");
- saddr.sin_port=htons(12345);
- ret=connect(sktfd,(struct sockaddr*)&saddr,sizeof(saddr));
- if(sktfd<0)
- {
- dispErrInfo("connect failed, ",errno);
- return errno;
- }
- while(1)
- {
- memset(txbuf,0,256);
- sprintf(txbuf,"hi there %d\n",x++);
- write(sktfd,txbuf,strlen(txbuf));
- sleep(1);
- }
- return 0;
- }
复制代码
在Windows主机中启动网络调试助手,以TCP服务器打开,测试效果如下:
|
|