• [国嵌攻略][093][并发服务器设计]


    server.c

    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <strings.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    #define SERVER_PORT 3333
    
    void main(){
     	//创建标识
    	int serverfd;
    	
    	serverfd = socket(AF_INET, SOCK_STREAM, 0);   //建立TCP连接
    	
    	//绑定地址
    	struct sockaddr_in serverAddr;
    	
    	serverAddr.sin_family = AF_INET;                   //设置协议
    	serverAddr.sin_port = htons(SERVER_PORT);          //设置端口
    	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);    //设置地址
    	bzero(serverAddr.sin_zero, 8);                     //设置为零
    	
    	bind(serverfd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr));
    	
    	//监听端口
    	listen(serverfd, 10);
    	
    	while(1){
    		//等待连接
    		socklen_t clientAddrLen;
    		struct sockaddr_in clientAddr;
    		int clientfd;
    
    		clientAddrLen = sizeof(struct sockaddr);
    		clientfd = accept(serverfd, (struct sockaddr *)&clientAddr, &clientAddrLen);
    		
    		//创建进程
    		int pid;
    		
    		pid = fork();
    		if(pid == 0){
    			//接收数据
    			char buffer[2048];
    			
    			recv(clientfd, buffer, sizeof(buffer), 0);
    			
    			//显示数据
    			char *clientIPAddr;
    			
    			clientIPAddr = (char *)inet_ntoa(clientAddr.sin_addr.s_addr);
    			printf("server recvice from %s:
    %s", clientIPAddr, buffer);
    			
    			//关闭连接
    			close(clientfd);
    			close(serverfd);
    			
    			//结束进程
    			exit(0);
    		}
    
    		//关闭连接
    		close(clientfd);
    	}
    	
    	//关闭连接
    	close(serverfd);
    }
    

    client.c

    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <strings.h>
    #include <stdio.h>
    
    #define SERVER_PORT 3333
    
    int main(int argc, char **argv){
    	//参数检查
    	if(argc != 2){
    		printf("Usage:
    	./client <ip address>
    ");
    		return -1;
    	}
    	
    	//创建标识
    	int clientfd;
    	
    	clientfd = socket(AF_INET, SOCK_STREAM, 0);   //建立TCP连接
    	
    	//建立连接
    	struct sockaddr_in serverAddr;
    	
    	serverAddr.sin_family = AF_INET;                   //设置协议
    	serverAddr.sin_port = htons(SERVER_PORT);          //设置端口
    	serverAddr.sin_addr.s_addr = inet_addr(argv[1]);   //设置地址
    	bzero(serverAddr.sin_zero, 8);                     //设置为零
    	
    	connect(clientfd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr));
    
    	//输入数据
    	char buffer[2048];
    	
    	printf("client send to %s:
    ", argv[1]);
    	fgets(buffer, sizeof(buffer), stdin);
    	
    	//发送数据
    	send(clientfd, buffer, sizeof(buffer), 0);
    	
    	//关闭连接
    	close(clientfd);
    	
    	return 0;
    }
    
  • 相关阅读:
    cocos2d-x v3.0的window平台搭建和编译成andriod程序
    学习笔记---C++伪函数(函数对象)
    多益网络2014校招的一道笔试题---左旋字符串
    学习笔记---C++虚函数,纯虚函数
    学习笔记---C++析构函数心得
    Cocos2d-x 3.0坐标系详解(转载)
    stack around the variable “XX” was corrupted
    Python学习目录
    Linux命令-案例:过滤出磁盘使用率超过80%的目录
    js 高级三 基础篇 (一)
  • 原文地址:https://www.cnblogs.com/d442130165/p/5233721.html
Copyright © 2020-2023  润新知