客户端与服务器端 socket通信示例代码_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > 客户端与服务器端 socket通信示例代码

客户端与服务器端 socket通信示例代码

 2012/2/28 18:02:54  hanyuanbo  程序员俱乐部  我要评论(0)
  • 摘要:以下代码是服务器端与客户端简单进行TCPsocket通信过程。Server.cpp#include<winsock2.h>#include<iostream>#pragmacomment(lib,"ws2_32.lib")usingnamespacestd;#defineBUFSIZE64#definePORT9999intmain(){WSADATAwsadata;SOCKETsServer;SOCKETsClient;intretVal
  • 标签:socket通信 代码 客户 服务器 客户端 服务 socket
以下代码是服务器端与客户端简单进行TCP socket通信过程。


Server.cpp

#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")

using namespace std;

#define BUFSIZE 64
#define PORT 9999
int main()
{
	WSADATA wsadata;
	SOCKET sServer;
	SOCKET sClient;
	int retVal;
	char buf[BUFSIZE];

	if(WSAStartup(MAKEWORD(2,2), &wsadata) != 0)
	{
		cout << "WSAStartup失败!" << endl;
		return -1;
	}
	
	sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);								/* ******************** 1 **************** */
	if (INVALID_SOCKET == sServer)
	{
		cout << "socket() 错误!" << endl;
		WSACleanup();
		return -1;
	}

	SOCKADDR_IN addrServ;
	addrServ.sin_family = AF_INET;
	addrServ.sin_port = htons(PORT);
	addrServ.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	retVal = bind(sServer, (const struct sockaddr*)&addrServ, sizeof(addrServ));		/* ******************** 2 **************** */
	if (SOCKET_ERROR == retVal)
	{
		cout << "bind 错误!" << endl;
		closesocket(sServer);
		WSACleanup();
		return -1;
	}

	retVal = listen(sServer,10);														/* ******************** 3 **************** */
	if (SOCKET_ERROR == retVal)
	{
		cout << "listen 错误!" << endl;
		closesocket(sServer);
		WSACleanup();
		return -1;
	}

	cout << "TCP服务器开始监听" << PORT << "端口" << endl;
	SOCKADDR_IN addrClient;
	int addrClientLen = sizeof(addrClient);
	sClient = accept(sServer,(sockaddr FAR*)&addrClient, &addrClientLen);				/* ******************** 4 **************** */
	if (INVALID_SOCKET == sClient)
	{
		cout << "accept() 错误!" << endl;
		WSACleanup();
		return -1;
	}

	while (true)
	{
		ZeroMemory(buf, BUFSIZE);
		retVal = recv(sClient, buf, BUFSIZE, 0);										/* ******************** 5 **************** */
		if (SOCKET_ERROR == retVal)
		{
			cout << "recv()错误!" << endl;
			closesocket(sServer);
			closesocket(sClient);
			WSACleanup();
			return -1;
		}

		SYSTEMTIME time;
		GetLocalTime(&time);
		char datetime[30];
		sprintf(datetime,"%4d-%2d-%2d %2d:%2d:%2d",time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
		cout << datetime << "从" << inet_ntoa(addrClient.sin_addr) << ":" << addrClient.sin_port << "接受了如下内容: " << buf << endl;

		if (strcmp(buf, "quit") == 0)
		{
			retVal = send(sClient, "quit", strlen("quit"), 0);
			break;
		}
		else
		{
			retVal = send(sClient, buf, strlen(buf), 0);								/* ******************** 6 **************** */
			if (SOCKET_ERROR == retVal)
			{
				cout << "send错误!" << endl;
				closesocket(sServer);
				closesocket(sClient);
				WSACleanup();
				return -1;
			}
		}
	}

	closesocket(sServer);
	closesocket(sClient);
	WSACleanup();

	return 0;
}



Client.cpp

#include <Winsock.h>
#include <string>
#include <iostream>

#pragma comment(lib, "ws2_32.lib")
using namespace std;


#define BUFSIZE 64
#define PORT 9999

int main()
{
	WSAData wsaData;
	SOCKET sHost;
	sockaddr_in addrServ;
	char buf[BUFSIZE];
	int retVal;
	
	if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
	{
		cout << "WSAStartup失败!" << endl;
		return -1;
	}
	
	sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == sHost)
	{
		cout << "socket() 错误!" << endl;
		WSACleanup();
		return -1;
	}
	
	addrServ.sin_family = AF_INET;
	addrServ.sin_port = htons(PORT);
	addrServ.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	
	retVal = connect(sHost, (LPSOCKADDR)&addrServ, sizeof(addrServ));
	if (SOCKET_ERROR == retVal)
	{
		cout << "connect 错误!" << endl;
		closesocket(sHost);
		WSACleanup();
		return -1;
	}
	
	while (true)
	{
		cout << "输入要发给服务器的内容" << endl;
// 		string msg;
// 		getline(cin, msg);
		char msg[BUFSIZE];
		cin.getline(msg, BUFSIZE);
		ZeroMemory(buf, BUFSIZE);
		strcpy(buf, msg);
		retVal = send(sHost, buf, strlen(buf), 0);
		if (SOCKET_ERROR == retVal)
		{
			cout << "发送失败" << endl;
			closesocket(sHost);
			WSACleanup();
			return -1;
		}
		
		retVal = recv(sHost, buf, sizeof(buf)+1, 0);
		cout << "从服务器端接收:" << buf << endl;
		if (strcmp(buf, "quit") == 0)
		{
			cout << "quit" << endl;
			break;
		}
	}
	
	closesocket(sHost);
	WSACleanup();
	
	
	return 0;
}





以下代码是服务器端与客户端简单进行UDP socket通信过程。
Server.cpp

#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")

using namespace std;

#define BUFSIZE 1024
#define PORT 9999
int main()
{
	WSADATA wsadata;
	SOCKET sRecv;
	sockaddr_in addrSend, addrRecv;
	int addrSendLen = sizeof(addrSend);
	char buf[BUFSIZE];

	if(WSAStartup(MAKEWORD(2,2), &wsadata) != 0)
	{
		cout << "WSAStartup失败!" << endl;
		return -1;
	}
	
	sRecv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	addrRecv.sin_family = AF_INET;
	addrRecv.sin_port = htons(PORT);
	addrRecv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	bind(sRecv, (SOCKADDR*) &addrRecv, sizeof(addrRecv));
	cout << "从客户端接收:" << endl;
	recvfrom(sRecv, buf, BUFSIZE, 0, (SOCKADDR *)&addrSend, &addrSendLen);


	cout << "完成接收:" << buf << endl;

	closesocket(sRecv);



	WSACleanup();

	return 0;
}

Client.cpp

#include <winsock2.h>
#include <iostream>
#include <string>
#pragma comment(lib, "ws2_32.lib")

using namespace std;

#define BUFSIZE 1024
#define PORT 9999
int main()
{
	WSADATA wsadata;
	SOCKET sSend;
	sockaddr_in addrRecv;
	char buf[BUFSIZE];
	
	if(WSAStartup(MAKEWORD(2,2), &wsadata) != 0)
	{
		cout << "WSAStartup失败!" << endl;
		return -1;
	}
	
	sSend = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	addrRecv.sin_family = AF_INET;
	addrRecv.sin_port = htons(PORT);
	addrRecv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	cout << "请输入要发送给服务器的内容:" << endl;
	cin.getline(buf, BUFSIZE);
	sendto(sSend, buf, BUFSIZE, 0, (SOCKADDR *)&addrRecv, sizeof(addrRecv));
	
	
	closesocket(sSend);
	
	
	
	WSACleanup();
	
	return 0;
}
发表评论
用户名: 匿名