欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tcp socket通信例子

程序员文章站 2022-04-24 13:27:41
...
tcpclient:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>

#define HOST_PORT 10000
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
int clientfd = socket(AF_INET, SOCK_STREAM, 0);
if(clientfd < 0)
{
printf("client create socket failed.\n");
return 0;
}
int on = 1;
if(setsockopt(clientfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) < 0)
{
printf("set socket option failed.\n");
return 0;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.56");

char buf[BUFSIZ];
memset(buf,0,BUFSIZ);
strncpy(buf,"shawn's client",14);
//buf[BUFSIZ] = '\0';

if(connect(clientfd,(const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("%d\n",errno);
return 0;
}
int index = 0;
for(index = 0; index < 5; index++)
{
send(clientfd,buf,strlen(buf) + 1, 0);
_sleep(10);
}
strcpy(buf,"end");
send(clientfd,buf,strlen(buf) + 1, 0);
closesocket(clientfd);

getchar();
return 0;
}
tcpserver:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define MAX_LISTEN_QUEUE_NUMBER 10
#define HOST_PORT 10000
#define MAX_PACKET_LENGTH 1400
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 0;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return 0;
}

int servfd, clientfd;
servfd = socket(AF_INET, SOCK_STREAM, 0);
if(servfd < 0)
{
printf("server create socket failed.\n");
return 0;
}
int on = 1;
if(setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) < 0)
{
printf("set socket option failed.\n");
return 0;
}
struct sockaddr_in servaddr;
struct sockaddr_in clientaddr;

memset(&servaddr,0,sizeof(servaddr));
memset(&clientaddr,0,sizeof(clientaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
if(bind(servfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("bind failed.\n");
return 0;
}
if(listen(servfd,10) < 0)
{
printf("listen failed.\n");
return 0;
}
char buf[MAX_PACKET_LENGTH];
memset(buf,0,MAX_PACKET_LENGTH);
int length = sizeof(clientaddr);

int clientRecord[FD_SETSIZE];
int index = 0;
for(; index < FD_SETSIZE; index++)
clientRecord[index] = -1;
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(servfd,&readSet);
int maxfd = servfd;

for(;;)
{
clientfd = accept(servfd, (struct sockaddr *)&clientaddr,&length);

for(index = 0; index < FD_SETSIZE; index++)
{
if(clientRecord[index] < 0)
{
clientRecord[index] = clientfd;
break;
}
}
if(index == FD_SETSIZE)
{
printf("too many sockets.\n");
break;
}
for(index = 0; index < FD_SETSIZE; index++)
{
if(clientRecord[index] > 0)
{
FD_SET(clientRecord[index],&readSet);
maxfd = maxfd > clientRecord[index] ? maxfd : clientRecord[index];
}
else
break;
}

struct timeval time;
time.tv_sec = 10;
time.tv_usec = 0;
if(select(maxfd,&readSet,NULL,NULL,&time) <= 0)
break;

int sockfd;

for(index = 0; index < FD_SETSIZE; index++)
{
if((sockfd = clientRecord[index]) < 0)
continue;
if(FD_ISSET(sockfd,&readSet))
{
while(1)
{
recv(sockfd,buf,MAX_PACKET_LENGTH,0);
printf("%s\n",buf);
if(strcmp(buf,"end") == 0)
break;
}
closesocket(sockfd);
clientRecord[index] = -1;
}
}
}
closesocket(servfd);
return 0;
}
看到一个评论,说的比较在理,于是重新改写了一下,起码做到了真正的非IO阻塞了吧:
tcpclient:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>

#define HOST_PORT 10000
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
int clientfd = socket(AF_INET, SOCK_STREAM, 0);
if(clientfd < 0)
{
printf("client create socket failed.\n");
return 0;
}
int on = 1;
if(setsockopt(clientfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) < 0)
{
printf("set socket option failed.\n");
return 0;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.56");

char buf[BUFSIZ];
memset(buf,0,BUFSIZ);
strncpy(buf,"shawn's client",14);
//buf[BUFSIZ] = '\0';

if(connect(clientfd,(const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("%d\n",errno);
return 0;
}
int index = 0;
for(index = 0; index < 5; index++)
{
send(clientfd,buf,strlen(buf) + 1, 0);
_sleep(10);
}

memset(buf,0,BUFSIZ);
strcpy(buf,"end");
send(clientfd,buf,strlen(buf) + 1, 0);
closesocket(clientfd);

getchar();
return 0;
}
tcpserver.c:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define MAX_LISTEN_QUEUE_NUMBER 10
#define HOST_PORT 10000
#define MAX_PACKET_LENGTH 1400
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 0;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return 0;
}

int servfd, clientfd;
servfd = socket(AF_INET, SOCK_STREAM, 0);
if(servfd < 0)
{
printf("server create socket failed.\n");
return 0;
}
int on = 1;
if(setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) < 0)
{
printf("set socket option failed.\n");
return 0;
}
struct sockaddr_in servaddr;
struct sockaddr_in clientaddr;

memset(&servaddr,0,sizeof(servaddr));
memset(&clientaddr,0,sizeof(clientaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
if(bind(servfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("bind failed.\n");
return 0;
}
if(listen(servfd,10) < 0)
{
printf("listen failed.\n");
return 0;
}
char buf[MAX_PACKET_LENGTH];
memset(buf,0,MAX_PACKET_LENGTH);
int length = sizeof(clientaddr);

int clientRecord[FD_SETSIZE];
int index = 0;
for(; index < FD_SETSIZE; index++)
clientRecord[index] = -1;
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(servfd,&readSet);
int maxfd = servfd;

for(;;)
{
struct timeval time;
time.tv_sec = 10;
time.tv_usec = 0;
if(select(maxfd,&readSet,NULL,NULL,&time) <= 0)
continue;
if(FD_ISSET(servfd,&readSet))
{
clientfd = accept(servfd, (struct sockaddr *)&clientaddr,&length);

for(index = 0; index < FD_SETSIZE; index++)
{
if(clientRecord[index] < 0)
{
clientRecord[index] = clientfd;
break;
}
}
if(index == FD_SETSIZE)
{
printf("too many sockets.\n");
break;
}
}
for(index = 0; index < FD_SETSIZE; index++)
{
if(clientRecord[index] > 0)
{
FD_SET(clientRecord[index],&readSet);
maxfd = maxfd > clientRecord[index] ? maxfd : clientRecord[index];
}
else
break;
}

int sockfd;

for(index = 0; index < FD_SETSIZE; index++)
{
if((sockfd = clientRecord[index]) < 0)
continue;
if(FD_ISSET(sockfd,&readSet))
{
while(1)
{
recv(sockfd,buf,MAX_PACKET_LENGTH,0);
printf("%s\n",buf);
if(strcmp(buf,"end") == 0)
break;
}
closesocket(sockfd);
clientRecord[index] = -1;
}
}
}
closesocket(servfd);
return 0;
}
相关标签: socket c