• 使用C编写一个简单的Socket聊天,有Server端和Client端


    一、概述

      利用C写一个客户端服务器模式的简单的Socket网络通讯服务,此服务特别简单:只需要一个Server对应一个Client的聊天,不支持多个Client和Server聊天,聊天模式也比较简单,就是发什么会回复什么。

      1.Server.cpp的创建步骤

        a.创建socket

        b.设置参数setsockopt

        c.绑定bind

        d.监听listen

        e.接收客户端发送过来的消息accept

        f.给客户端回复消息send

        g.关闭链接 close

      2.Client端的创建步骤

        a.创建socket

        b.连接服务端 connect

        c.给服务端发送消息 send

        d.接收来自服务端的消息recv

        f.关闭socket

      3.执行成功后手动编译或者放工具编辑都可。在client终端输入要发送的内容,在终端就会显示发送内容以及服务端回复的内容。

    二、代码示例

      1.client代码  

    #include <iostream>
    
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <string.h>
    #include <stdlib.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    
    #define MESSAGE_LEN  1024
    #define PORT 8888
    using namespace std;
    
    int main(int argc, char const *argv[])
    {
    	/* code */
    	//创建socket
    	int socket_fd;
    	int ret=-1;
    	struct sockaddr_in serveraddr;
    	char sendbuf[MESSAGE_LEN] = {0,};
    	char recvbuf[MESSAGE_LEN] = {0,};
    	socket_fd = socket(AF_INET,SOCK_STREAM,0);
    	if(socket_fd==-1){
    			cout << ""<<endl;
    			exit(-1);
    	}
    	serveraddr.sin_family = AF_INET;
    	serveraddr.sin_port = PORT;
    	serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    	ret = connect(socket_fd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
    	if(ret<0){
    		cout<<"Failed to connect server!"<<endl;
    		exit(-1);
    	}
    	while(1){
    		memset(sendbuf,0,MESSAGE_LEN);
    		gets(sendbuf);
    		ret = send(socket_fd,sendbuf,strlen(sendbuf),0);
    		if(ret<=0){
    			cout<<"Failed tosend data!"<<endl;
    			break;
    		}
    		if(strcmp(sendbuf,"quit")==0){
    			break;
    		}
    		ret = recv(socket_fd,recvbuf,MESSAGE_LEN,0);
    		recvbuf[ret] = '';
    		cout << "recv:"<<recvbuf<<endl
    		;
    
    
    
    	}
    	close(socket_fd);
    	return 0;
    }
    

      

      2.server代码  

    #include <iostream>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    
    #define PORT 8888
    #define MESSAGE_LEN 1024
    int main(int argc, char const *argv[])
    {
    	
    	int socket_fd,accept_fd;
    	int on= 1;
    	int ret =-1;
    	int backlog = 10;
    	struct sockaddr_in localaddr ,remoteaddr;
    	socket_fd = socket(AF_INET,SOCK_STREAM,0);
    
    	char in_buff[MESSAGE_LEN] = {0};
    	//如果socket创建失败就退出
    	if(socket_fd==-1){
    		std::cout<<"Failed to create socket!"<<std::endl;
    		exit(-1);
    	}
    
    	ret = setsockopt(
    		socket_fd,
    		SOL_SOCKET,
    		SO_REUSEADDR,
    		&on,
    		sizeof(on));
    	if(ret==-1){
    		std::cout<<"Failed 通setsocket options!"<<std::endl;
    		exit(-1);
    	}
    
    	localaddr.sin_family=AF_INET;
    	localaddr.sin_port = PORT;
    	localaddr.sin_addr.s_addr = INADDR_ANY;
    	bzero(&(localaddr.sin_zero),8);
    
    
    	ret = bind(socket_fd,
    		(struct sockaddr*)&localaddr,sizeof(struct sockaddr));
    	if(ret  ==-1){
    		std::cout<< "Failed to bind addr!"<<std::endl;
    		exit(-1);
    	}
    
    	ret = listen(socket_fd,backlog);
    	if(ret==-1){
    		std::cout<< "Failed to bind listen socket!"<<std::endl;
    		exit(-1);
    	}
    	for(;;){
    		socklen_t addr_len = sizeof(struct sockaddr);
    		accept_fd = accept(socket_fd,(struct sockaddr*)&remoteaddr,&addr_len);
    		for(;;){
    			ret=recv(accept_fd,(void*)in_buff,MESSAGE_LEN,0);
    			if(ret==0){
    				break;
    			}
    			std::cout << "recv:"<< in_buff<<std::endl;
    			send(accept_fd,(void*)in_buff,MESSAGE_LEN,0);
    			
    		}
    		close(accept_fd);
    		
    
    	}
    	close(socket_fd);
    	return 0;
    }
    

      

  • 相关阅读:
    ASP VNext 开源服务容错处理库Polly使用文档
    .NET Core开源API网关 – Ocelot中文文档
    Open ID Connect(OIDC)在 ASP.NET Core中的应用
    ASP.NET Core 系列视频完结,新项目实战课程发布。
    这应该是目前最快速有效的ASP.NET Core学习方式(视频)
    极简版ASP.NET Core学习路径及教程
    全面理解 ASP.NET Core 依赖注入
    向ASP.NET Core迁移
    ASP.NET Core集成现有系统认证
    Gitlab CI 自动部署 asp.net core web api 到Docker容器
  • 原文地址:https://www.cnblogs.com/tony-yang-flutter/p/15378760.html
Copyright © 2020-2023  润新知