码迷,mamicode.com
首页 > 其他好文 > 详细

c socket 开发测试

时间:2015-05-19 18:38:30      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

c语言异常

参照他人代码写一个tcp的 socket 开发测试

异常A,在mac osx系统下编译失败,缺库转到debian下。

异常B,include引用文件顺序不对,编译大遍异常

异常C,/usr/include/x86_64-linux-gnu/sys/types.h:34:1: error: unknown type name ‘__u_char’ 文件前注释的问题,删掉注释则通过

 

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> 
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
int accepthandle(int acceptpoint);
int main(void) {
    int socketfid;
    char buf[1024];
    struct sockaddr_in  server_add;
    char * localaddr = "127.0.0.1";//
    server_add.sin_family=AF_INET;
    server_add.sin_port=htons(29001);
    server_add.sin_addr.s_addr=htonl(INADDR_ANY);
    
    socketfid=socket(AF_INET,SOCK_STREAM,0);
    printf("socket start \n");
    if (socketfid < 0) {
        printf("socket error \n");
        return -1;
    }
    printf("bind start \n");
    if(bind(socketfid,(struct sockaddr *)&server_add,sizeof(struct sockaddr))<0){
        printf("bind error \n");
        return -1;
    }
    //监听套接子
    printf("listen start \n");
    if(listen(socketfid,1024)<0){
        printf("listen error \n");
        return -1;
    }
    printf("accept start \n");
    while(1){
        int pid;
        struct sockaddr_in client_add;
        memset(&client_add, 0, sizeof(client_add));
        int leng=sizeof(client_add);

        int acceptresult=accept(socketfid,(struct sockaddr *) &client_add,&(leng));
        if (acceptresult < 0) {
            printf("accept error %d \n", acceptresult);
            return -1;
        }
        printf("infor \n");
        printf("clent addr%s porit %d\n",
               inet_ntop(AF_INET, &client_add.sin_addr, buf, sizeof(buf)),
               ntohs(client_add.sin_port));
        pid = fork();
        if (pid < 0) {
            close(acceptresult);
        }
        else if(pid == 0){
            //子进程停止监听,去处理接收数据
            close(socketfid);
            accepthandle(acceptresult);
        } else {
            //父进程 什么都不干(继续监听)
        }
    }
    return EXIT_SUCCESS;
}
int accepthandle(int acceptpoint){
    char buf[1024];
    int readresult;
    while(1){
        readresult=read(acceptpoint,buf,sizeof(buf));
        if(readresult<0){
            printf("read error \n");
            close(acceptpoint);
            break;
        }
        else if (readresult==0){
            printf("client exit \n");
            close(acceptpoint);
            break;
        }
        else{
            printf("client:%s\n", buf);
            if (strcmp("exit", buf) == 0) {
                printf("exit \n");
                close(acceptpoint);
                return 0;
            }
            
        }
        
        
    }
    return 0;
}

 

c socket 开发测试

标签:

原文地址:http://www.cnblogs.com/zihunqingxin/p/4515147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!