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

📄 client.cpp

📁 类似QQ的通信的简单服务器和客户端程序
💻 CPP
字号:
// Client.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Client.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define PORTNUM         5000          // Port number


/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;
#include <winsock.h>

using namespace std;
UINT t_LNKRecv(LPVOID pParam)
{
	int		iReturn;
	SOCKET	ServerSock;
	char szClientA[100];                // ASCII string 
	ServerSock = (SOCKET)pParam;

	while(TRUE)
	{
		iReturn = recv (ServerSock, szClientA, sizeof (szClientA), 0);
		// Check if there is any data received. If there is, display it.
		if (iReturn == SOCKET_ERROR)
		{
			printf("Error");
			break;
		}
		else if (iReturn == 0)
		{
			printf("\nFinished receiving data");
			break;
		}
		else
		{
		// Display the string received from the server.
		printf ("\nReceive from serve:\n%s\n",szClientA);
		}
	}
	return 0;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) return 1;
	int index = 0;                      // Integer index
//		iReturn;                        // Return value of recv function
	char szClientA[100];                // ASCII string 
//	TCHAR szError[100];                 // Error message string
	
	SOCKET ServerSock = INVALID_SOCKET; // Socket bound to the server
	SOCKADDR_IN destination_sin;        // Server socket address
	PHOSTENT phostent = NULL;           // Points to the HOSTENT structure
                                      // of the server
	WSADATA WSAData;                    // Contains details of the 
                                      // Winsocket implementation

	// Initialize Winsocket. 
	if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0) 
	{
		printf("Initialize Winsocket Error\n");
		return FALSE;
	}
	else
	{
	    printf("Initialize Winsocket Succeed\n");
	}
	// Create a TCP/IP socket that is bound to the server.
	if ((ServerSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
	{
		printf("Create a TCP/IP soket Error\n");
		return FALSE;
	}
	else
	{ 
		printf("Create a TCP/IP succeed\n");
	}
	cout<< "\nPlease Input Server IP:>";

	char szServerIp[32];
    cin>>szServerIp;
	destination_sin.sin_family				= AF_INET;
	destination_sin.sin_addr.S_un.S_addr	= inet_addr(szServerIp);
	destination_sin.sin_port				= htons (PORTNUM);

	// Establish a connection to the server socket.
	if (connect (ServerSock, 
				(PSOCKADDR) &destination_sin, 
				sizeof (destination_sin)) == SOCKET_ERROR) 
	{
		printf("Establish a connection to the server socket Error\n");
		closesocket (ServerSock);
		return FALSE;
	}
	else
	{ 
		printf("Establish a connection to the server socket succeed\n");
		CWinThread* ServerRecv;
		ServerRecv	= AfxBeginThread(t_LNKRecv		,(LPVOID)ServerSock,THREAD_PRIORITY_NORMAL,CREATE_SUSPENDED ,0, NULL );
		
	}
	while(TRUE)
	{
		cout<< endl<<"Input message to send>"<<endl;
		gets(szClientA);
		if (send (ServerSock, szClientA, strlen (szClientA) + 1, 0)== SOCKET_ERROR) 
		{
			printf("Send a string to the server Error");
		}
		else
		{ 
		    printf("Send a string to the server succeed\n");
			
		}
	}

	shutdown (ServerSock, 0x00);

	// Close the socket.
	closesocket (ServerSock);

	WSACleanup ();

	return 0;
}


⌨️ 快捷键说明

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