c_cpp tcp_server

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp tcp_server相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <err.h>

#define MAXMSGLEN 100

int main(int argc, char**argv) {
	char *msg="Hello from server";
	char buf[MAXMSGLEN+1];
	char *serverport;
	unsigned short port;
	int sockfd, sessfd, rv, i;
	struct sockaddr_in srv, cli;
	socklen_t sa_size;
	
	// Get environment variable indicating the port of the server
	serverport = getenv("serverport15440");
	if (serverport) port = (unsigned short)atoi(serverport);
	else port=15440;
	
	// Create socket
	sockfd = socket(AF_INET, SOCK_STREAM, 0);	// TCP/IP socket
	if (sockfd<0) err(1, 0);			// in case of error
	
	// setup address structure to indicate server port
	memset(&srv, 0, sizeof(srv));			// clear it first
	srv.sin_family = AF_INET;			// IP family
	srv.sin_addr.s_addr = htonl(INADDR_ANY);	// don't care IP address
	srv.sin_port = htons(port);			// server port

	// bind to our port
	rv = bind(sockfd, (struct sockaddr*)&srv, sizeof(struct sockaddr));
	if (rv<0) err(1,0);
	
	// start listening for connections
	rv = listen(sockfd, 5);
	if (rv<0) err(1,0);
	
	// main server loop, handle clients one at a time, quit after 10 clients
	for( i=0; i<10; i++ ) {
		
		// wait for next client, get session socket
		sa_size = sizeof(struct sockaddr_in);
		sessfd = accept(sockfd, (struct sockaddr *)&cli, &sa_size);
		if (sessfd<0) err(1,0);
		
		// get messages and send replies to this client, until it goes away
		// recv ==0 means socket close
		while ( (rv=recv(sessfd, buf, MAXMSGLEN, 0)) > 0) { 
			buf[rv]=0;		// null terminate string to print
			printf("server got messge: %s\n", buf);
			
			// send reply
			printf("server replying to client: %s\n", msg);
			send(sessfd, msg, strlen(msg), 0);	// should check return value
		}

		// either client closed connection, or error
		if (rv<0) err(1,0);
		close(sessfd);
	}
	
	printf("server shutting down cleanly\n");
	// close socket
	close(sockfd);

	return 0;
}

以上是关于c_cpp tcp_server的主要内容,如果未能解决你的问题,请参考以下文章

Boost tcp_server async_write 错误:访问冲突写入位置

远程执行命令

粘包2

python-利用socket客户端服务端循环收发消息

利用tcp协议实现远程控制

前端1-----HTML了解,内联标签(图片,超链接锚点,超链接邮箱)