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

网络编程实例代码

时间:2016-05-23 18:52:07      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

Makefile:

all:
	gcc -o server server.c -lpthread
	gcc -o client client.c

clean:
	rm server client

server.c:

/*TCP:
  server.c
*/

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

void *do_work(void *arg);

int main(int argc,char *argv[])
{
    int sockfd;
    int ret;

    /*1.调用socket建立网络套接字*/
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(-1 == sockfd){
        perror("socket server");
        return -1;
    }

    /*2.填充服务器地址和端口号*/
    struct sockaddr_in servaddr;
    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family = AF_INET;        //协议族
    servaddr.sin_port = htons(6775);    //端口号5001-65536
    servaddr.sin_addr.s_addr = inet_addr("192.168.7.223"); //ip地址
    socklen_t alen = sizeof(servaddr);

    /*3.调用bind绑定服务器地址和端口*/
    ret = bind(sockfd,(struct sockaddr *)&servaddr,alen);    
    if(-1 == ret){
        perror("bind");
        return -1;
    }

    /*4.调用listen主动监听是否有客户连接*/
    ret = listen(sockfd,10);
    if(-1 == ret){
        perror("listen");
        return -1;
    }
    printf("servet init.d success...\n");

    int newfd = -1;     //新的套接字描述符
    char buf[1024] = {0};
    pthread_t tid;        //新建线程ID

    while(1)
    {
        /*5.调用accept接受客户端连接*/
        newfd = accept(sockfd,NULL,NULL);
        if(newfd <= 0){
            continue;
        }
        printf("==>%d connect success..\n",newfd);

        /*为每个客户开辟一个线程去管理*/
        ret = pthread_create(&tid,NULL,do_work,&newfd);
        if(0 != ret){
            perror("pthread_create failed\n");
            break;
        }
        pthread_detach(tid);//设置为分离态,资源由系统自动回收

    }

    /*7.关闭打开的网络套接字***/
    close(sockfd);
    close(newfd);

    return 0;
}

/*线程执行函数*/
void *do_work(void *arg)
{
    int newfd = *(int *)arg;
    int ret;
    char buf[1024];

    /*6.调用read/write读写网络文件*/
    while(1)
    {
        memset(buf,0,sizeof(buf));
        ret = read(newfd,buf,sizeof(buf)-1);    
        if(ret > 0){
            printf("recv data:%s\n",buf);
        }
    }

    pthread_exit(NULL);
}

client.c:

/*TCP:
  client.c
*/

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

int main(int argc,char *argv[])
{
    int sockfd;
    int ret;
    char buf[1024] = {0};

    /*1.调用socket建立网络套接字*/
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(-1 == sockfd){
        perror("socket server");
        return -1;
    }

    /*2.填充服务器地址和端口号*/
    struct sockaddr_in servaddr;
    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family = AF_INET;        //协议族
    servaddr.sin_port = htons(6775);    //端口号5001-65536
    servaddr.sin_addr.s_addr = inet_addr("192.168.7.223"); //ip地址
    socklen_t alen = sizeof(servaddr);

    /*3.调用connect连接服务器(根据ip地址和端口去连接)*/
    ret = connect(sockfd,(struct sockaddr *)&servaddr,alen);
    if(-1 == ret){
        perror("connect");
        return -1;
    }
    printf("client connect success...\n");

    /*4.调用read/write读写网络文件*/
    while(1)
    {
        /*从终端获取信息*/
        memset(buf,0,sizeof(buf));
        ret = read(0,buf,sizeof(buf)-1);    
        if(ret > 0){
            
            /*发送给服务器*/
            write(sockfd,buf,ret);
        }
    }

    /*7.关闭打开的网络套接字***/
    close(sockfd);

    return 0;
}

 

网络编程实例代码

标签:

原文地址:http://www.cnblogs.com/yihujiu/p/5520939.html

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