Java教程

跨平台网络编程:多线程服务器

本文主要是介绍跨平台网络编程:多线程服务器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef WIN32
#include <windows.h>
#define socklen_t int
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define closesocket close
#endif

#include <thread>
using namespace std;

class TcpThread
{
public:
	void Main()
	{
		char buf[1024] = { 0 };
		for (;;) {
			int recvlen = recv(client, buf, sizeof(buf) - 1, 0);
			if (recvlen <= 0) break;
			buf[recvlen] = '\0';
			if (strstr(buf, "quit") != NULL) {
				char re[] = "quit success!\n";
				send(client, re, strlen(re) + 1, 0);
				break;
			}
			int sendlen = send(client, "ok\n", 4, 0);
			printf("recv %s\n", buf);
		}
		closesocket(client);
		delete this;
	}
	int client = 0;
};

int main(int argc, char* argv[])
{	
#ifdef WIN32
	WSADATA ws;
	WSAStartup(MAKEWORD(2, 2), &ws);
#endif
	int sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1) {
		printf("create socket failed!");
		return -1;
	}
	unsigned short port = 8080;
	if (argc > 1) {
		port = atoi(argv[1]);//atoi 函数 字符串转整型
	}
	sockaddr_in saddr;
	saddr.sin_family = AF_INET;

	//小端 低位字节在前面,高位字节在后面 大端 低位字节在后面,高位字节在前面	 X86系统都是小端的 所以需要进行转换  网络字节序用的是大端的方式
	saddr.sin_port = htons(port);
	saddr.sin_addr.s_addr = htonl(0);

	//::bind() 表示是全局的 而不是std命名空间下的
	if (::bind(sock, (sockaddr*)&saddr, sizeof(saddr)) != 0) {
		printf("bind port %d failed!\n", port);
		return -2;
	}
	printf("bind port %d success!\n", port);
	listen(sock, 10);//要理解第二个参数的含义
	for (;;) {
		sockaddr_in caddr;
		socklen_t len = sizeof(caddr);
		int client = accept(sock, (sockaddr*)&caddr, &len);//三次握手不是在accept实现的,而是由系统内部完成的,accept只是读取三次握手后的信息
		if(client <= 0) break;
		printf("accept client %d\n", client);
		char* ip = inet_ntoa(caddr.sin_addr);
		unsigned short cport = ntohs(caddr.sin_port);//端口号最大65535
		printf("client ip is %s, port is %d\n", ip, cport);
		TcpThread* th = new TcpThread();
		th->client = client;
		thread sth(&TcpThread::Main, th);
		sth.detach();
	}
	closesocket(sock);
	getchar();
	return 0;
}

这篇关于跨平台网络编程:多线程服务器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!