zoukankan      html  css  js  c++  java
  • Tcp/Ip协议理解_简单实例

    #include <stdio.h>
    #include<sys/socket.h>
    #include <sys/types.h>
    #include<arpa/inet.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <fcntl.h>
    
    #define MYPORT 6666
    #define QUEUE 20
    #define BUFFER_SIZE 1024
    
    int main() {
    
        int server_socket = socket(AF_INET,SOCK_STREAM,0);
    
        struct sockaddr_in server_sockaddr;
        server_sockaddr.sin_family = AF_INET;
        server_sockaddr.sin_port = htons(MYPORT);
        server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        if(bind(server_socket,(struct sockaddr*)&server_sockaddr, sizeof(server_sockaddr))==-1)
        {
            perror("listen");
            exit(1);
        }
        if(listen(server_socket,QUEUE)==-1){
            exit(1);
        }
        char buffer[BUFFER_SIZE];
        struct sockaddr_in client_addr;
        socklen_t length = sizeof(client_addr);
        int conn = accept(server_socket,(struct sockaddr*)&client_addr,&length);
    
        if(conn<0)
        {
            perror("connert");
            exit(1);
        }
        while (1)
        {
            memset(buffer,0, sizeof(buffer));
            if (strcmp(buffer,"exit
    ")==0){
                break;
            }
            fputs(buffer,stdout);
            send(conn,buffer,length,0);
        }
        close(conn);
        close(server_socket);
    
    
        return 0;
    }
  • 相关阅读:
    MSSQL数据库 事务隔离级别
    CSS(Cascading Style Shee)
    Winform MD5
    Winform 基础
    ASP.NET 设置DropDownList的当前选项
    如何彻底关闭退出vmware虚拟机
    Winform GDI+
    SQL优化
    登录
    Spring AOP的应用
  • 原文地址:https://www.cnblogs.com/jingchu/p/10000406.html
Copyright © 2011-2022 走看看