使用C语言编写基于TCP协议的Socket通讯程序实例分享
tcp客户端示例
#include<errno.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
typedefstruct_NSS_HEADER
{
unsignedshortProtocolVersion;/*协议版本信息*/
unsignedshortMsgType;/*消息类型*/
unsignedshortTransactionNo;/*传输编号*/
unsignedshortPacketLength;/*数据包长度,包括Header*/
}NSS_HEADER;
intstr_echo(intsockfd,unsignedshortno)
{
ssize_treadLen;
ssize_twriteLen;
charbuf[8];
NSS_HEADER*hdr=(NSS_HEADER*)buf;
memset(hdr,0,sizeof(NSS_HEADER));
hdr->TransactionNo=no;
//写数据
intnWriteLen=0;
while(true)
{
writeLen=write(sockfd,&buf[nWriteLen],sizeof(NSS_HEADER)-nWriteLen);
//printf("write%d/n",writeLen);
if(writeLen<0&&errno==EINTR)
{
continue;
}
elseif(writeLen<0)
{
perror("write:");
return-1;
}
nWriteLen+=writeLen;
//已写完,直接返回
if(nWriteLen>=sizeof(NSS_HEADER))
{
break;
}
}
printf("senddatasuccessed.transno:%d/n",no);
//读数据
intnReadLen=8;
while(true)
{
readLen=read(sockfd,buf,nReadLen);
//printf("read:%d/n",readLen);
if(readLen<0&&errno==EINTR)
{
continue;
}
elseif(readLen<=0)
{
perror("read:");
return-1;
}
else
{
nReadLen-=readLen;
if(nReadLen<=0)
{
break;
}
}
}
printf("readresponsesuccessed./n");
return0;
}
intmain(intargc,char**argv)
{
printf("clientip:%s/n",argv[1]);
printf("clientport:%s/n",argv[2]);
printf("serverip:%s/n",argv[3]);
printf("serverport:%s/n",argv[4]);
printf("/nservicestarting.../n/n");
while(true)
{
intsocketFd;
structsockaddr_insvrAddr;
structsockaddr_inlocalAddr;
socketFd=socket(AF_INET,SOCK_STREAM,0);
if(-1==socketFd)
{
perror("socket:");
continue;
}
//设置地址可复用
intoption=1;
setsockopt(socketFd,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option));
//客户端IP
memset(&localAddr,0,sizeof(localAddr));
localAddr.sin_family=AF_INET;
localAddr.sin_addr.s_addr=inet_addr(argv[1]);
localAddr.sin_port=htons(atoi(argv[2]));
intbindResult=bind(socketFd,(structsockaddr*)&localAddr,sizeof(localAddr));
if(-1==bindResult)
{
perror("bind:");
sleep(10);
close(socketFd);
continue;
}
//服务器IP
memset(&svrAddr,0,sizeof(svrAddr));
svrAddr.sin_family=AF_INET;
svrAddr.sin_addr.s_addr=inet_addr(argv[3]);
svrAddr.sin_port=htons(atoi(argv[4]));
//不断重连
intconnResult=connect(socketFd,(structsockaddr*)&svrAddr,sizeof(svrAddr));
if(-1==connResult)
{
perror("connect:");
sleep(10);
close(socketFd);
continue;
}
printf("connect%s:%ssuccessed./n",argv[3],argv[4]);
staticunsignedshortno=0;
//连接成功,每分钟发送一次数据
for(;;)
{
if(-1==str_echo(socketFd,no++))
{
break;
}
sleep(60);
}
close(socketFd);
}
}
tcp服务器源码示例
#include<errno.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
struct_NSS_HEADER
{
unsignedshortProtocolVersion;/*协议版本信息*/
unsignedshortMsgType;/*消息类型*/
unsignedshortTransactionNo;/*传输编号*/
unsignedshortPacketLength;/*数据包长度,包括Header*/
}NSS_HEADER;
voidstr_echo(intsockfd)
{
ssize_treadLen;
ssize_twriteLen;
charbuf[8];
while(true)
{
readLen=read(sockfd,buf,8);
if(readLen<0&&errno==EINTR)
{
continue;
}
elseif(readLen<=0)
{
perror("read:");
return;
}
printf("recvdatasuccessed.datalen:%d/n",readLen);
intnWriteLen=0;
while(true)
{
writeLen==write(sockfd,&buf[nWriteLen],readLen-nWriteLen);
if(writeLen<0&&errno==EINTR)
{
continue;
}
elseif(writeLen<0)
{
perror("write:");
return;
}
nWriteLen+=writeLen;
//已写完,直接返回
if(nWriteLen>=readLen)
{
break;
}
}
printf("senddatasuccessed.datalen:%d/n",readLen);
}
}
intmain(intargc,char**argv)
{
printf("serverip:%s/n",argv[1]);
printf("serverport:%s/n",argv[2]);
printf("/nservicestarting.../n/n");
intlistenfd,connfd;
pid_tchildpid;
socklen_tclilen;
structsockaddr_incliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(-1==listenfd)
{
perror("socket:");
exit(-1);
}
//设置地址可复用
intoption=1;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option));
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(atoi(argv[2]));
intbindResult=bind(listenfd,(structsockaddr*)&servaddr,sizeof(servaddr));
if(-1==bindResult)
{
perror("bind:");
exit(-1);
}
intlistenResult=listen(listenfd,5);
if(-1==listenResult)
{
perror("listen:");
exit(-1);
}
for(;;)
{
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(structsockaddr*)&cliaddr,&clilen);
if(-1==connfd)
{
perror("accept:");
continue;
}
printf("accept%ssuccessed.fd:%d/n",inet_ntoa(cliaddr.sin_addr),connfd);
if((childpid=fork())==0)
{/*childprocess*/
close(listenfd);/*closelisteningsocket*/
str_echo(connfd);/*processtherequest*/
printf("disconnectfrom%d./n",connfd);
exit(0);
}
}
close(connfd);/*parentclosesconnectedsocket*/
}