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;
}
分享到:
相关推荐
本篇文章将详细介绍如何在Android平台上进行TCP Socket通信的封装,并结合实际例子进行说明。 首先,我们需要创建一个`SocketClient`类,作为客户端来连接服务端。这个类通常包含以下组件: 1. **初始化连接**:在...
本篇文章将围绕“MFC TCP Socket通信”的主题,详细解释这个简单小例子中的关键知识点。 首先,TCP是一种面向连接的、可靠的传输协议,它确保数据的无序传输得到纠正,丢失的数据会被重传,而重复的数据则会被丢弃...
基于Qt实现的TCP socket通信,这是我学习qt socket通信自己写的一个小例子,希望对你有所帮助。整个文件包括服务端和客户端工程代码,具有如下功能: 1. 服务端支持多个客户端与之连接; 2. 服务端支持客户端...
在QT中,TCP Socket通信是网络编程的重要部分,允许程序间通过Internet进行数据传输。本示例将深入讲解如何在QT中实现TCP Socket通信。 首先,TCP(Transmission Control Protocol)是一种面向连接的、可靠的、基于...
下面将详细介绍这个简单的C++ TCP Socket通信例子,并探讨相关知识点。 首先,我们要理解TCP的基本概念。TCP是一种面向连接的、可靠的传输协议,它通过三次握手建立连接,并在数据传输完成后通过四次挥手断开连接。...
总的来说,"VC 2008网络编程的例子,TCP socket Server例子之2"是一个实践性的教学示例,旨在帮助开发者掌握在Windows环境下使用C++进行TCP网络通信的技能。通过分析和运行这个例子,你可以更好地理解TCP套接字...
在IT行业中,TCP/IP协议族是网络通信的基础,而Socket编程则是实现TCP/IP通信的关键接口。这个"Tcp_ip socket 简单例子"提供了一个学习基础Socket编程的起点,特别适合初学者。在这个实例中,多线程技术被用于提高...
本示例将深入解析"C# Socket tcp 最简单例子源码",帮助你理解如何在C#中实现TCP(传输控制协议)客户端和服务端的通信。 首先,TCP是一种面向连接的、可靠的、基于字节流的传输层通信协议。它确保数据包按顺序无损...
C#中的异步Socket通信主要依赖两个关键方法:`BeginConnect`和`EndConnect`用于建立连接,`BeginSend`和`EndSend`用于发送数据,以及`BeginReceive`和`EndReceive`用于接收数据。 1. **建立异步连接** 使用`...
在IT行业中,网络通信是不可或缺的一部分,而TCP(Transmission Control Protocol)和Socket是实现网络通信的基础。本教程将深入探讨如何使用C#语言编写原生的TCP Socket客户端和服务端程序。TCP是一种面向连接的、...
Socket通信在IT领域中是一种基础且重要的网络编程技术,它允许程序通过网络发送和接收数据。VB.NET(Visual Basic .NET)是微软开发的一种面向对象的编程语言,它支持使用Socket进行网络通信。本篇文章将深入探讨VB...
首先,TcpClient和TcpSocket是.NET Framework提供的两种用于TCP(传输控制协议)通信的类。TcpClient是更高层次的抽象,它提供了一种更加方便的方式来创建和管理TCP连接,而TcpSocket则更接近底层,提供了更多的...
通过研究这个例子,你可以深入了解如何在VC 2008环境下编写TCP服务器,以及如何处理客户端的连接和数据交换。这对于学习网络编程和开发实际的多用户应用程序非常有帮助。记得在实践中不断调试和完善,以提升你的网络...
本示例"**C# TCP/IP通信小例子**"是一个基础教程,旨在帮助新手理解如何在C#中实现TCP/IP通信。 TCP(传输控制协议)和IP(因特网协议)是Internet上最基础的通信协议,TCP负责确保数据的可靠传输,而IP则处理...
QT点对点SOCKET通信是网络编程中的一个重要概念,它主要涉及到使用QT库进行TCP/IP通信的技术。在本文中,我们将深入探讨QT如何实现点对点的SOCKET通信,以及在局域网环境下如何进行基本的socket编程。首先,我们需要...
这个最简单的Java Socket通信例子展示了基本的TCP连接流程,但实际应用中可能需要考虑异常处理、多线程处理多个客户端连接、更复杂的数据格式化等高级特性。通过这个基础例子,你可以深入学习Java网络编程,为构建更...
这个压缩包可能包含了一个示例代码,演示了如何在Linux服务器端使用epoll处理TCP连接,并在Windows客户端进行相应的socket通信。通过分析和运行这个例子,可以深入理解epoll和TCP在实际应用中的工作原理和实现方式。
本例子就是Delphi中如何利用Socket编写通信程序。 计算机网络是由一系列网络通信协议组成的,其中的核心协议是传输层的TCP/IP和UDP协议。TCP是面向连接的,通信双方保持一条通路,好比目前的电话线,使用telnet登陆...
在QT框架中,C++ Socket通信是实现网络数据交换的基础,它允许客户端和服务端通过网络进行数据传输。本文将深入探讨如何在QT中使用C++编写客户端和服务端的socket通信代码,以及涉及到的关键操作。 首先,我们需要...