计算机网络-多人聊天编程实验

【实验题目】多人聊天编程实验
【实验目的】掌握套节字的多线程编程方法。
【实验介绍】利用客户/服务器(Client/Sever或CS)模式实现一个多人聊天(群聊)程序。其功能是每个客户发送给服务器的消息都会传送给所有的客户端。

【实验环境】
采用Windows VC++控制台编程,建立新项目的方法:选择菜单“文件/新建项目/Win32控制台应用程序/空项目”,然后增加CPP空文件,拷贝源程序。集成环境使用VS2012或更高版本。

【实验内容】

(1)编写多人聊天程序,要求客户端和服务器都采用多线程方式进行编程。每个客户端都采用TCP协议连接服务器并保持连接。服务器同时与所有客户端建立和保持连接。每个客户端输入的消息都会通过服务器转发给所有客户。

(2)服务器程序转发某个客户端发来的消息时都在消息前面加上该客户端的IP地址和端口号以及服务器的当前时间。要求服务器程序把转发的消息也显示出来。

(3)新客户刚连接时服务器端把“enter”消息(包含客户端IP地址和端口号)发送给所有客户端。

(4)客户端输入exit时退出客户端程序(正常退出),或者客户端直接关闭窗口退出(异常退出),服务器都会把该客户leave的消息广播给所有客户。

客户端程序

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define    BUFLEN        2000                  // 缓冲区大小
#define WSVERS        MAKEWORD(2, 0)        // 指明版本2.0 
#pragma comment(lib,"ws2_32.lib")         // 指明winsock 2.0 Llibrary

/*------------------------------------------------------------------------
 * main - TCP client for DAYTIME service
 *------------------------------------------------------------------------
 */

SOCKET    sock, sockets[100] = { NULL };                          /* socket descriptor            */
//    int    cc;                                /* recv character count            */
char    *packet = NULL;               /* buffer for one line of text    */
char *pts, *input;
HANDLE hThread;
unsigned threadID;

unsigned int __stdcall Chat(PVOID PM)
{
	time_t    now;
	(void)time(&now);
	pts = ctime(&now);
	char buf[2000];

	while (1)
	{
		int cc = recv(sock, buf, BUFLEN, 0);   //cc为接收的字符数
		if (cc == SOCKET_ERROR || cc == 0)
		{
			printf("Error: %d.----", GetLastError());
			printf("与服务器断开连接!\n");
			CloseHandle(hThread);
			(void)closesocket(sock);
			break;
		}
		else if (cc > 0)
		{
			//    buf[cc] = '\0';
			//printf("与服务器连接成功!\n");
			printf("%s\n", buf);

			//    printf("输入数据(exit退出):  \n");
		}
	}
	return 0;
}

int main(int argc, char *argv[])
{
	time_t    now;
	(void)time(&now);
	pts = ctime(&now);
	const char    *host = "127.0.0.1";        /* server IP to connect         */
	//const char    *host = "172.26.54.208";
	const char *service = "5050";          /* server port to connect       */
	//const char *service = "50520";
	struct  sockaddr_in sin;            /* an Internet endpoint address    */
	WSADATA wsadata;
	WSAStartup(WSVERS, &wsadata);       /* 启动某版本Socket的DLL        */

	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons((u_short)atoi(service));    //atoi:把ascii转化为int. htons:主机序(host)转化为网络序(network), s--short
	sin.sin_addr.s_addr = inet_addr(host);           //如果host为域名,需要先用函数gethostbyname把域名转化为IP地址

	sock = socket(PF_INET, SOCK_STREAM, 0);

	connect(sock, (struct sockaddr *)&sin, sizeof(sin));

	printf("\t\t\t\t   客户端启动     \n");

	hThread = (HANDLE)_beginthreadex(NULL, 0, Chat, NULL, 0, &threadID);

	while (1)
	{
		char buf1[2000];

		//     scanf("%s",&buf1);
		//printf("可以聊天了\n\n");
		//printf("请输入发送内容:");
		gets_s(buf1);
		//printf("%s\n",buf1);
		if (!strcmp(buf1, "exit"))
			goto end;

		(void)send(sock, buf1, sizeof(buf1), 0);
		(void)time(&now);
		pts = ctime(&now);

		printf("发送成功!\n\n");
		//printf("时间: %s\n", pts);
	}

end:    CloseHandle(hThread);
	closesocket(sock);
	WSACleanup();                     /* 卸载某版本的DLL */

	printf("按回车键继续...");
	getchar();
	return 0;                           /* exit */
}

服务端程序

#include 
#include 
#include 
#include 
#include "conio.h"
#include 
#include 
#include 

#define QLEN       5
#define    WSVERS    MAKEWORD(2, 0)
#define    BUFLEN    2000             // 缓冲区大小
#pragma comment(lib,"ws2_32.lib")  //winsock 2.2 library

SOCKET    msock, ssock;            /* master & slave sockets           */
SOCKET    sockets[100] = { NULL };

int cc;
char    *pts;                    /* pointer to time string           */
time_t    now;                    /* current time                       */
char buf[2000];                      /* buffer                          */
char *input;
HANDLE hThread1, hThread[100] = { NULL };
unsigned int threadID, ThreadID[100], number;

struct    sockaddr_in fsin;
struct    sockaddr_in Sin;

unsigned int __stdcall Chat(PVOID PM)
{
	char buf1[2000];
	char buf2[2000];
	char buf3[2000];
	char buf4[2000];
	(void)time(&now);
	pts = ctime(&now);
	sockets[number] = ssock;
	SOCKET    sock = ssock;
	ThreadID[number] = threadID;
	unsigned int threadid = threadID;
    
	
	sprintf(buf2, "客户【IP:%s 端口:%d】:enter  \n时间:%s\n", inet_ntoa(fsin.sin_addr), fsin.sin_port,pts);
	
	//sprintf(buf2, "Client:enter \nIP:%s \nPort:%d \ntime:%s\n", s, fsin.sin_port, pts);
	printf("%s ", buf2);
	
	for (int i = 0; i <= number; i++)
	{
		if (sockets[i] != NULL )
		{
			(void)send(sockets[i], buf2, sizeof(buf2), 0);
			
		}
	}
	printf(" \n");


flag1:cc = recv(sock, buf3, BUFLEN, 0);   //cc为接收的字符数
	 if (cc == SOCKET_ERROR || cc == 0)
	{
		(void)time(&now);
		pts = ctime(&now);
		
		sprintf(buf3, "客户【IP:%s 端口:%d】:leave !  \n时间:%s", inet_ntoa(fsin.sin_addr), fsin.sin_port, pts);
		sock = NULL;
		sockets[number] = NULL;
		CloseHandle(hThread[number]);
		printf("%s ", buf3);
		
		for (int i = 0; i <= number; i++)
		{
			if (sockets[i] != NULL && sockets[i] != sock)
			{
				(void)send(sockets[i], buf3, sizeof(buf3), 0);
				
			}
		}
		printf(" \n");
	}

	else if (cc > 0)
	{
		(void)time(&now);
		pts = ctime(&now);
		sprintf(buf4, "客户【IP:%s 端口:%d】说:%s\n时间:%s", inet_ntoa(fsin.sin_addr), fsin.sin_port,buf3, pts);
		
		printf("%s ", buf4);
		
		for (int i = 0; i <= number; i++)
		{
			if (sockets[i] != NULL)
			{
				(void)send(sockets[i], buf4, sizeof(buf4), 0);
				
			}
		}
		printf(" \n");

		goto flag1;
	}
	(void)closesocket(sock);

	return 0;
}


/*------------------------------------------------------------------------
 * main - Iterative TCP server for DAYTIME service
 *------------------------------------------------------------------------
 */
void main(int argc, char *argv[])
/* argc: 命令行参数个数, 例如:C:\> TCPdaytimed 8080
					 argc=2 argv[0]="TCPdaytimed",argv[1]="8080" */
{
	int     alen;                    /* from-address length               */
	WSADATA wsadata;
	const char    *service = "5050";
	WSAStartup(WSVERS, &wsadata);                         //加载 winsock 2.2 library
	msock = socket(PF_INET, SOCK_STREAM, 0);              //生成套接字。TCP协议号=6, UDP协议号=17
	memset(&Sin, 0, sizeof(Sin));
	Sin.sin_family = AF_INET;
	Sin.sin_addr.s_addr = INADDR_ANY;                    //指定绑定接口的IP地址。INADDR_ANY表示绑定(监听)所有的接口。
	Sin.sin_port = htons((u_short)atoi(service));        //atoi--把ascii转化为int,htons - 主机序(host)转化为网络序(network), s(short) 
	bind(msock, (struct sockaddr *)&Sin, sizeof(Sin)); // 绑定端口号(和IP地址)

	listen(msock, 5);                                    //队列长度为5

	printf("\t\t\t\t服务器启动 \n");
	
	
	(void)time(&now);
	pts = ctime(&now);
	printf("\t\t\t\t时间 :%s\n", pts);
	
	number = -1;
	while (1)                                    //检测是否有按键
	{
		alen = sizeof(struct sockaddr);
		ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
		number++;
		hThread[number] = (HANDLE)_beginthreadex(NULL, 0, Chat, NULL, 0, &threadID);
	}
	(void)closesocket(msock);
	WSACleanup();                         //卸载载 winsock 2.2 library
}

你可能感兴趣的:(计算机网络)