您的位置:首页 > 其它

socket通信简单例子

2010-12-29 14:33 387 查看
在看unix网络编程,闲着的时候采用TCP实现了一个简单的cs通信示例:

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(;;)
{
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;
}


tcpClient.c

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: