Socket模拟客户端与服务器

原创 2015年07月09日 22:47:24


代码如下:
#include <stdio.h>
#include <WinSock2.h>
#include <process.h>
#pragma comment(lib, "ws2_32.lib")


HANDLE g_hEvent = NULL;
CRITICAL_SECTION g_crs;
void myPrintf(const char* psz1, ...)
{
	::EnterCriticalSection(&g_crs);
	va_list arg;
	va_start(arg, psz1);
	vprintf(psz1, arg);
	va_end(arg);
	::LeaveCriticalSection(&g_crs);
}


unsigned __stdcall ClientProc(void* parm)
{
	WSADATA wsData;
	SOCKET skClient;
	SOCKADDR_IN addrServ;
	char msg[1024]={0};
	fd_set rFSet;
	int iRet = WSAStartup(MAKEWORD(2, 2), &wsData);
	if (iRet != 0)
	{
		myPrintf(">>> client WSAStartup fail\n");
		return 1;
	}
	skClient = socket(AF_INET, SOCK_STREAM, 0);
	myPrintf(">>> client socket %d\n", skClient);
	addrServ.sin_family = AF_INET;
	addrServ.sin_port = htons(6000);
	addrServ.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	WaitForSingleObject(g_hEvent, INFINITE);
	iRet = connect(skClient, (sockaddr*)&addrServ, sizeof(SOCKADDR_IN));
	if (iRet != 0)
	{
		myPrintf(">>> client connect fail\n");
		goto end;
	}
	while (true)
	{
		::EnterCriticalSection(&g_crs);
		printf(">>> client please input send msg: ");
		scanf("%s", msg);
		::LeaveCriticalSection(&g_crs);
		iRet = send(skClient, msg, sizeof(msg)/sizeof(char), 0);
		if (iRet == SOCKET_ERROR)
		{
			myPrintf(">>> client send fail\n");
			break;
		}
		FD_ZERO(&rFSet);
		FD_SET(skClient, &rFSet);
		iRet = select(skClient + 1, &rFSet, NULL, NULL, NULL);
		if (iRet <= 0)
		{
			myPrintf(">>> client select fail\n");
			break;
		}
		memset(msg, 0, sizeof(msg)/sizeof(char));
		iRet = recv(skClient, msg, sizeof(msg)/sizeof(char), 0);
		if (iRet == SOCKET_ERROR)
		{
			myPrintf(">>> client recv fail\n");
			break;
		}
		if (strcmp(msg, "quit") == 0)
		{
			myPrintf(">>> client quit\n");
			break;
		}
		else
		{
			myPrintf(">>> client recv: %s\n", msg);
		}
	}
	closesocket(skClient);
end:
	myPrintf(">>> client exit\n");
	WSACleanup();
	return 0;
}




unsigned __stdcall ServerProc(void* param)
{
	WSADATA wsData;
	int iRet = WSAStartup(MAKEWORD(2, 2), &wsData);
	if (iRet != 0)
	{
		myPrintf("server WSAStartup fail\n");
		return 1;
	}
	char msg[1024]={0};
	int len = sizeof(SOCKADDR_IN);
	SOCKET skServer, skClient;
	fd_set rfSet;
	skServer = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
	SOCKADDR_IN addrServer, addrClient;
	addrServer.sin_family = AF_INET;
	addrServer.sin_port = htons(6000);
	addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	iRet = bind(skServer, (const sockaddr*)&addrServer, sizeof(addrServer));
	if (iRet != 0)
	{
		myPrintf(">>> server bind fail\n");
		goto end;
	}
	iRet = listen(skServer, 5);
	if (iRet == -1)
	{
		myPrintf(">>> server listen fail\n");
		goto end;
	}
	SetEvent(g_hEvent);
	skClient = accept(skServer, (sockaddr*)&addrClient, &len);
	if (skClient == -1)
	{
		myPrintf(">>> no client connect to server\n");
		goto end;
	}
	myPrintf(">>> client %d connect to server success\n", skClient);
	while (true)
	{
		FD_ZERO(&rfSet);
		FD_SET(skClient, &rfSet);
		iRet = select(skClient + 1, &rfSet, NULL, NULL, NULL);
		if (iRet <= 0)
		{
			myPrintf(">>> server select fail\n");
			break;
		}
		memset(msg, 0, sizeof(msg)/sizeof(char));
		iRet = recv(skClient, msg, sizeof(msg)/sizeof(char), 0);
		if (iRet == SOCKET_ERROR)
		{
			myPrintf(">>> server recv fail\n");
			break;
		}
		else
		{
			myPrintf(">>> server recv: %s\n", msg);
			::EnterCriticalSection(&g_crs);
			myPrintf(">>> server please input command: ");
			memset(msg, 0, sizeof(msg)/sizeof(char));
			scanf("%s", msg);
			::LeaveCriticalSection(&g_crs);
			iRet = send(skClient, msg, sizeof(msg)/sizeof(char), 0);
			if (iRet == SOCKET_ERROR)
			{
				myPrintf(">>> server send fail\n");
				break;
			}
		} 
	}
end:
	closesocket(skServer);
	WSACleanup();
	myPrintf(">>> server exit\n");
	return 0;
}




int _tmain(int argc, _TCHAR* argv[])
{
	InitializeCriticalSection(&g_crs);
	g_hEvent = CreateEventA(NULL, TRUE, FALSE, "MMClient##Event");
	HANDLE hClient = (HANDLE)_beginthreadex(NULL, 0, ClientProc, NULL, 0, NULL);
	HANDLE hServer = (HANDLE)_beginthreadex(NULL, 0, ServerProc, NULL, 0, NULL);
	WaitForSingleObject(hClient, INFINITE);
	WaitForSingleObject(hServer, INFINITE);
	system("@pause");
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

C语言 Socket入门示例2——模拟远程CMD(客户端向服务器发送命令,服务端执行该命令)

只要把上一篇文章“C语言 Socket入门示例1”中的两段程序彻底搞懂,那么再看本文就没有任何难度了,因为仅仅是对上篇文章中服务端代码的简单修改扩充。但是简单修改过后,功能变得异常强大,犹如一个远程C...

Java Socket 模拟客户端与服务器端通讯

SocketServer.java

java之Socket模拟服务器端与客户端

服务器端: import java.io.*; import java.net.*; public class xin { ServerSocket mSS; Socket mSocket;...

Socket服务器和客户端

  • 2013-05-23 16:54
  • 46KB
  • 下载

Java Socket实现多客户端通信(通过DOS命令窗口模拟)

服务器端代码/* * * 当客户端、服务器端产生了对应的socket以后,就得到了双方的通信。程序无须再区分服务器端、客户端, * 而是通过各自的Socket进行通信。 * * Inpu...

Java----Socket编程之模拟客户端Client

以Java为例,Socket和ServerSocket类库位于java.net包中。ServerSocket用于服务器端,Socket是建立网络连接时使用的。本文介绍了java Socket的建立,数...
  • Roy_70
  • Roy_70
  • 2017-03-06 22:32
  • 2294
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)