⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sever.cpp

📁 基于Socket的服务端和客户端程序
💻 CPP
字号:
// Sever.cpp : 定义控制台应用程序的入口点。
//

//该程序建立了一个套接字,然后开始无限循环,每当他通过循环收到一个连接便打印
//出一个信息。当连接断开时或接收到终止信息,则此连接结束。程序再接受一个新的
//连接。命令行格式为stream

#include "stdafx.h"
#include "winsock2.h"
#include "stdio.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int				receiveFlag;
	char			dataReceiveBuf[20];//数据缓冲
	char			dataSendBuf[5];
	int				port = 5150;//端口
	int				clientAddrLen;//客户地址长度
	int				sendFlag;

	WSADATA			wsaData;
	SOCKET			listeningSocket;//监听socket
	SOCKET			newConnection;
	SOCKADDR_IN		serverAddr;//服务器地址
	SOCKADDR_IN		clientAddr;//客户地址
	
	//initialize the Windows Socket 2.2
	 if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
	 {
		 perror("Cannot initialize the Windows Socket 2.2 !");
		 exit(1);
	 }

	//create a socket to listen the ask of the client
	listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(listeningSocket == INVALID_SOCKET)
	{
		printf("Socket failed with error %d \n", WSAGetLastError());
		WSACleanup();
		exit(1);
	}

	//fill in the address information of the Server
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(port);
	serverAddr.sin_addr.s_addr = inet_addr("192.168.2.107");//htonl(INADDR_ANY);

	//bind the listening port
	if( bind(listeningSocket, (SOCKADDR *)&serverAddr, 
		sizeof(serverAddr)) == SOCKET_ERROR)
	{
		printf("Bind failed with error %d \n", WSAGetLastError());
		closesocket(listeningSocket);
		WSACleanup();
		exit(1);
	}

	//begin listening 
	if( listen(listeningSocket, 5) == SOCKET_ERROR )
	{
		printf("Listen failed with error %d \n", WSAGetLastError());
		closesocket(listeningSocket);
		WSACleanup();
		exit(1);
	}

	printf("We are awaiting a connection on port %d \n", port);
	
	while(1)
	{
		clientAddrLen = sizeof(clientAddr);
		newConnection = accept(listeningSocket, (SOCKADDR *)&clientAddr,&clientAddrLen);
		if(newConnection == SOCKET_ERROR)
		{
			printf("Accept failed: %d \n", WSAGetLastError());
		}
		else
		{
			printf("Accept successfully!\n");
			memset(dataReceiveBuf, 0, sizeof(dataReceiveBuf));//将数据缓冲器置零
			do
			{				
				//接收数据
				receiveFlag = recv(newConnection, dataReceiveBuf, sizeof(dataReceiveBuf), 0);
				
				if(receiveFlag < 0)
				{//接收不正常
					printf("break\n");
					break;
				}
				else if(receiveFlag == 0)
				{//连接断开
					printf("Ending connection\n");					
				}
				else
				{//接收完成,receiveFlag保存了接收到的数据个数
					//打印接收到的数据
					printf("the data come from client are these:\n");
					int i;
					for(i = 0; i < (receiveFlag - 1); i++)
						{
							printf("%c-", dataReceiveBuf[i]);
							dataSendBuf[i] = dataReceiveBuf[i];
						}
					printf("%c", dataReceiveBuf[i]);
					printf("\n");

					sendFlag = send(newConnection, dataSendBuf, (int)sizeof(dataSendBuf), 0);
					if(sendFlag == INVALID_SOCKET)
					{
						printf("send error!");
						break;
					}
				}				
			}while(receiveFlag != 0);

			
			//关闭当前socket
			closesocket(newConnection);
		}		
	}

	closesocket(listeningSocket);
	WSACleanup();

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -