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通信 server端
推荐阅读
-
libevent 实现的socket 通信 server以及解决找不到动态库的方法
-
常用类之TCP连接类-socket编程
-
4.智能快递柜(通信篇-SOCKET)
-
python实现简单socket通信的方法
-
详解Android 基于TCP和UDP协议的Socket通信
-
【转载】启动redis出现Creating Server TCP listening socket *:6379: bind: No such file or directory
-
Python基于socket模块实现UDP通信功能示例
-
Python网络编程使用select实现socket全双工异步通信功能示例
-
Python两台电脑实现TCP通信的方法示例
-
python3利用Socket实现通信的方法示例