linux下socket编程学习

最近今天在看socket编程,参考网上的例子,自己写了一个服务器回射程序,服务器端想用线程处理消息,但是线程这块还没想好怎么弄,现在只能规定一个最大的连接次
数,超过这个次数服务器就结束了。

下面是代码:

server.c

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

#define MAX_LEN 100
#define PORT_NUM 4321
#define EXIT_FAIL_NUM 1
#define MAX_NUM 3


void trans(void *p);

struct trans_data{
    int fd;
    char buff[MAX_LEN];
    struct sockaddr_in info;
};

int main(){
    int sockfd,confd,len,err,i=0;
    pthread_t thread[MAX_NUM];
    struct sockaddr_in ser,cli;
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
        perror("socket");
        exit(EXIT_FAIL_NUM);
    }
    printf("scoket_fd = %d\n",sockfd);
    bzero(&ser,sizeof(ser));
    ser.sin_family = AF_INET;
    ser.sin_port = htons(PORT_NUM);
    ser.sin_addr.s_addr = htonl(INADDR_ANY);
    if(-1 == bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))){
        perror("bind");
        exit(EXIT_FAIL_NUM);
    }
    if(0 != listen(sockfd,10)){
        perror("listen");
        exit(EXIT_FAIL_NUM);
    }
    printf("Receive message......\n");
    while(i<MAX_NUM){
        len = sizeof(cli);
        confd = accept(sockfd,(struct sockaddr *)&cli,&len);
        if(-1 == confd){
            perror("accept");
            exit(EXIT_FAIL_NUM);
        } 


        struct trans_data p;
        p.fd = confd;
        memcpy(&p.info,&cli,sizeof(p.info));
        err = pthread_create(&thread[i],NULL,(void *)&trans,(void *)&p);
        if(0 != err){
            printf("Can not create thread!\n");
            exit(EXIT_FAIL_NUM);
        }
        i++;
    }
    return 0;
}


void trans(void *p){
    struct trans_data *ptr;
    ptr = (struct trans_data *)p;
    while(1){
        memset((*ptr).buff,0,sizeof((*ptr).buff));
        int len;
        len = recv((*ptr).fd,(*ptr).buff,sizeof((*ptr).buff),0);
        if(len ==0){
            pthread_exit(NULL);
        }
        printf("Message from:\n IP address:%s\t",inet_ntoa((struct in_addr )(*ptr).info.sin_addr));
        printf("port number=%d\n",(*ptr).info.sin_port);
        puts((*ptr).buff);
    }
}

client.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<error.h>
#include<errno.h>

#define MAX_LEN 100
#define EXIT_FAIL 1
#define PORT_NUM 4321

int main(){
    int sockfd,confd,len;
    char buff[MAX_LEN];
    struct sockaddr_in cli,ser;
    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
        perror("socket");
        exit(EXIT_FAIL);
    }
    printf("socket_fd = %d\n",sockfd);
    bzero(&ser,sizeof(struct sockaddr_in));
    ser.sin_family = AF_INET;
    ser.sin_addr.s_addr = inet_addr("127.0.0.1");
    ser.sin_port = htons(PORT_NUM);
    printf("s_addr = %#x,port:%#x\r\n",ser.sin_addr.s_addr,ser.sin_port);
    if((confd = connect(sockfd,(struct sockaddr *)&ser,sizeof(struct sockaddr))) == -1){
        perror("connect");
        exit(EXIT_FAIL);
    }
    printf("client connected......\n");
    memset(buff,0,sizeof(buff));
    while(scanf("%s",buff)!=EOF){
        len = send(sockfd,buff,strlen(buff),0);
        if(len == -1){
            perror("send");
            exit(EXIT_FAIL);
        }
        memset(buff,0,sizeof(buff));
    }
    close(sockfd);
    return 0;
}
您的支持将鼓励我继续创作!