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;
}


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

客户端发送xml数据,服务器端接收之

package com.xiuye.xml; import java.io.InputStream; import java.net.ServerSocket; import java.net.So...

【问题解决】HttpClient解析服务器返回的response出现乱码

问题场景最近在用httpClient做网络爬虫的时候,遇到了一个不大不小的问题,当使用HttpGet向指定网址发送请求后,接收到的Response无法正常解析,出现 口口??这样的乱码,编码也考虑到了...

Java混乱器Zelix KlassMaster逆向工程(二)软件运行流程以及爆破去除时间限制

    对于Zelix KlassMaster(简称ZKM)有一个时间限制,有固定的过期时间。为了对ZKM进行破解,可以通过反编译的方式进行。一般的软件首先要找到入口点,然后分析软件的流程。1、软件Z...
  • vhly
  • vhly
  • 2007年02月02日 14:43
  • 2460

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

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

C# socket通信 模拟服务器端、客户端

项目名---右键-----调试-------启动新实例(实现开启多个项目) socket用户描述IP地址和端口,是一个通信链的句柄。 将远程连接的客户端的IP地址和Socket存入Dirtionary...

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
  • 下载

使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置)

Silverlight 与服务器利用Socket通讯,实时从服务器获取数据(本文中的数据是地理坐标),由于没有GPS,所以本文在服务器写了一个构造新坐标的函数(本文是一个三角函数),然后利用Timer...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Socket模拟客户端与服务器
举报原因:
原因补充:

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