首页 > 编程语言 >tcp网络编程实例1

tcp网络编程实例1

时间:2023-03-14 23:00:12浏览次数:119  
标签:perror socket int 编程 tcp 实例 fd include serviceaddr

tcp_server.c

#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>

//int socket(int domain, int type, int protocol);


//typedef void (*sighandler_t)(int);
//sighandler_t signal(int signum, sighandler_t handler);

//void perror(const char *s);

int socket_fd;

void sig_fun(int signo)
{
    if(signo == SIGINT){
        printf("signal = %d\n",signo);
        close(socket_fd);
        exit(1);
    }
    
}
void out_addr(struct sockaddr_in *clientaddr)
{
    int port = ntohs(clientaddr->sin_port);
    
    char ip[16];
    
    if(inet_ntop(AF_INET, &clientaddr->sin_addr.s_addr, ip ,16) == NULL){
        perror("out_addr:");
    }
    
    printf("cliet:%s(%d)\n",ip,port);
}

void do_service(int fd)
{
    //获得系统时间
    /*long t = time(0);
    char *s = ctime(&t);
    size_t size = sizeof(s);*/
    
    char *s = "hello world";
    //将时间写给客户端
    if(write(fd, s, sizeof("hello world")) != sizeof("hello world")){
        perror("write:");
    }
}

int main(int argc,char *argv[])
{
    if(argc <2){
        perror("argc<2:");
        exit(1);
    }
    
    if(signal(SIGINT,sig_fun) == SIG_ERR){
        perror("signal:");
        exit(1);
    }
    
    /*
    *1. 创建socket
    */
    if((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("socket:");
        exit(1);
    }
    /*
    *2.绑定IP地址和端口号
    *int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
    */
    struct sockaddr_in serviceaddr;
    memset(&serviceaddr, 0, sizeof(serviceaddr));
    serviceaddr.sin_family = AF_INET;
    serviceaddr.sin_port = htons(atoi(argv[1]));
    serviceaddr.sin_addr.s_addr = INADDR_ANY;//一台服务器上可能有多块网卡(多个IP地址)
    //这个宏是响应本机所有网卡(IP地址)上连接的客户端请求
    
    if(bind(socket_fd, (struct sockaddr *)&serviceaddr, sizeof(serviceaddr)) < 0){
        perror("bind:");
        exit(1);
    };
    
    /*
    *3.监听绑定的端口
    *通知系统去接受来自客户端的连接请求
    *(将接受到的客户端连接请求放置到对应的队列中)
    *第二个参数:指定队列的长度
    */
    if(listen(socket_fd, 10) < 0){
        perror("listen:");
        exit(1);
    }
    
    /*
    *4.调用accept函数从队列中获得一个客户端的连接请求
    *并返回一个新的socket描述符,这个描述符和和客户端
    *的连接请求对应,即和客户端对应
    *如果没有客户端连接,调用此函数会阻塞,直到获得一个客户端的连接
    *第二个参数:客户端的地址信息
    */
    struct sockaddr_in clientaddr;
    socklen_t clientaddr_len = sizeof(clientaddr);
    while(1){
        int fd = accept(socket_fd, (struct sockaddr *)&clientaddr, &clientaddr_len);
        if(fd < 0){
            perror("accept:");
            continue;
        }
        /*
        *5.调用read/write函数
        */
        out_addr(&clientaddr);
        do_service(fd);
        
        /*
        *6.关闭socket
        */
        close(fd);
        
    }
    
    return 0;
}       
tcp_client.c

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>

int main(int argc, char *argv[])
{
    int socket_fd;
    if(argc < 2){
        printf("参数太少");
        exit(1);
    }
    //1.创建socket
    if((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("socket:");
        exit(1);
    }
    
    //2. 连接服务器
    struct sockaddr_in serviceaddr;
    memset(&serviceaddr, 0, sizeof(serviceaddr));
    serviceaddr.sin_family = AF_INET;
    serviceaddr.sin_port = htons(atoi(argv[2]));
    if(inet_pton(AF_INET, argv[2], &serviceaddr.sin_addr.s_addr) < 0){
        perror("inet_pton:");
        exit(1);
    } 
    if(connect(socket_fd, (struct sockaddr *)&serviceaddr, sizeof(serviceaddr)) < 0){
        perror("connect");
        exit(1);
    }
    
    //3. read/write
    char buff[1024];
    memset(buff, 0, 1024);
    size_t size = read(socket_fd, buff, 1024);
    
    if(write(STDOUT_FILENO, buff, size) != size){
        perror("write");
        exit(1);
    }
    
    //关闭套接字
    close(socket_fd);
        
    return 0;
}

 

标签:perror,socket,int,编程,tcp,实例,fd,include,serviceaddr
From: https://www.cnblogs.com/zj-studyrecoding/p/17216807.html

相关文章