네트워크 프로그래밍 코드 실례

47959 단어
가장 간단한 서버, 클라이언트 연결:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8888


int main()
{
    int sockfd, ret, fd;
    char buf[100] = {0};
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;

    printf("Start Server!
"
); sockfd = socket(PF_INET, SOCK_STREAM, 0); if(-1 == sockfd) { perror("socket"); exit(1); } memset(&server_addr, 0 ,sizeof(server_addr)); server_addr.sin_family = PF_INET; server_addr.sin_port = PORT; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if(-1 == ret) { perror("bind"); exit(1); } printf("Listening...
"
); ret = listen(sockfd,5); if(-1 == ret) { perror("listen"); exit(1); } int length = sizeof(client_addr); fd = accept(sockfd, (struct sockaddr *)&client_addr, &length); if(-1 == fd) { perror("accept"); exit(1); } while(1) { ret = recv(fd,buf,sizeof(buf),0); if(-1 == ret) { perror("recv"); exit(1); } if(!strcmp(buf,"bye")) { break; } printf("Recv From Client : %s
"
,buf); memset(buf, 0, sizeof(buf)); } close(sockfd); close(fd); return 0; }
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8888

int main()
{
    int sockfd, ret;
    struct sockaddr_in server_addr;
    char buf[100] = {0};

    sockfd = socket(PF_INET, SOCK_STREAM, 0);
    if(-1 == sockfd)
    {
        perror("socket");
        exit(1);
    }

    memset(&server_addr, 0 ,sizeof(server_addr));
    server_addr.sin_family = PF_INET;
    server_addr.sin_port = PORT;
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    printf("Start Connecting...
"
); ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if(-1 == ret) { perror("connect"); exit(1); } while(1) { scanf("%s",buf); ret = send(sockfd, buf, strlen(buf), 0); if(-1 == ret) { perror("send"); exit(1); } if(!strcmp(buf,"bye")) { break; } memset(buf, 0, sizeof(buf)); } close(sockfd); return 0; }

서버에 메시지를 보낼 클라이언트를 만들려면 다음과 같이 하십시오.
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8888


void *MyReceive(void *arg)
{
    char buf[100] = {0};
    int ret;

    pthread_detach(pthread_self());

    while(1)
    {
        ret = recv(*(int *)arg,buf,sizeof(buf),0);
        if(-1 == ret)
        {
            perror("recv");
            exit(1);
        }
        if(!strcmp(buf,"bye"))
        {
            close(*(int *)arg);
            break;
        }

        printf("Recv From Client %d : %s
"
,*(int *)arg,buf); memset(buf,0,sizeof(buf)); } } int main() { int fd[100] = {0}; int length , i = 0; int sockfd, ret; char buf[100] = {0}; struct sockaddr_in server_addr; struct sockaddr_in client_addr; pthread_t tid[100] = {0}; printf("Start Server!
"
); sockfd = socket(PF_INET, SOCK_STREAM, 0); if(-1 == sockfd) { perror("socket"); exit(1); } memset(&server_addr, 0 ,sizeof(server_addr)); server_addr.sin_family = PF_INET; server_addr.sin_port = PORT; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if(-1 == ret) { perror("bind"); exit(1); } printf("Listening...
"
); ret = listen(sockfd,5); if(-1 == ret) { perror("listen"); exit(1); } while(1) { length = sizeof(client_addr); fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length); if(-1 == fd[i]) { perror("accept"); exit(1); } printf("Accept %d, Port %d
"
,fd[i],client_addr.sin_port); ret = pthread_create(&tid[i],NULL,MyReceive,(void *)&fd[i]); if(0 != ret) { perror("pthread_create"); exit(1); } i++; } close(sockfd); close(fd); return 0; }

#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8888

struct info
{
    char buf[100];
    int Tofd;
};

void *MyReceive(void *arg)
{
    struct info Recvbuf = {0};
    int ret;

    pthread_detach(pthread_self());

    while(1)
    {
        ret = recv(*(int *)arg,&Recvbuf,sizeof(Recvbuf),0);
        if(-1 == ret)
        {
            perror("recv");
            exit(1);
        }
      /*  if(!strcmp(buf,"bye"))
        {
            close(*(int *)arg);
            break;
        }

        printf("Recv From Client %d : %s
",*(int *)arg,buf);*/
ret = send(Recvbuf.Tofd,&Recvbuf,sizeof(Recvbuf),0); if(-1 == ret) { perror("send"); exit(1); } memset(&Recvbuf,0,sizeof(Recvbuf)); } } int main() { int fd[100] = {0}; int length , i = 0; int sockfd, ret; char buf[100] = {0}; struct sockaddr_in server_addr; struct sockaddr_in client_addr; pthread_t tid[100] = {0}; printf("Start Server!
"
); sockfd = socket(PF_INET, SOCK_STREAM, 0); if(-1 == sockfd) { perror("socket"); exit(1); } memset(&server_addr, 0 ,sizeof(server_addr)); server_addr.sin_family = PF_INET; server_addr.sin_port = PORT; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if(-1 == ret) { perror("bind"); exit(1); } printf("Listening...
"
); ret = listen(sockfd,5); if(-1 == ret) { perror("listen"); exit(1); } while(1) { length = sizeof(client_addr); fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length); if(-1 == fd[i]) { perror("accept"); exit(1); } printf("Accept %d, Port %d
"
,fd[i],client_addr.sin_port); ret = pthread_create(&tid[i],NULL,MyReceive,(void *)&fd[i]); if(0 != ret) { perror("pthread_create"); exit(1); } i++; } close(sockfd); // close(fd); return 0; }
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8888

struct info
{
    char buf[100];
    int Tofd;
};

pthread_t tid[2] = {0};

void *Send(void *arg)
{
    struct info Sendbuf = {0};
    int ret;

    while(1)
    {
        scanf("%s%d",Sendbuf.buf,&Sendbuf.Tofd);

        ret = send(*(int *)arg,&Sendbuf, sizeof(Sendbuf), 0);
        if(-1 == ret)
        {
            perror("send");
            exit(1);
        }

        /*if(!strcmp(buf,"bye"))
        {
            break;
        }*/

        memset(&Sendbuf, 0, sizeof(Sendbuf));
    }
}

void *Recv(void *arg)
{
    struct info Recvbuf = {0};
    int ret;

    while(1)
    {
        ret = recv(*(int *)arg,&Recvbuf,sizeof(Recvbuf),0);
        if(-1 == ret)
        {
            perror("recv");
            exit(1);

        }

        printf("\t\t\t%s
"
,Recvbuf.buf); memset(&Recvbuf,0,sizeof(Recvbuf)); } } int main() { int sockfd, ret; struct sockaddr_in server_addr; // char buf[100] = {0}; sockfd = socket(PF_INET, SOCK_STREAM, 0); if(-1 == sockfd) { perror("socket"); exit(1); } memset(&server_addr, 0 ,sizeof(server_addr)); server_addr.sin_family = PF_INET; server_addr.sin_port = PORT; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); printf("Start Connecting...
"
); ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if(-1 == ret) { perror("connect"); exit(1); } ret = pthread_create(&tid[0],NULL,Send,(void *)&sockfd); if(0 != ret) { perror("pthread_create1"); exit(1); } ret = pthread_create(&tid[1],NULL,Recv,(void *)&sockfd); if(0 != ret) { perror("pthread_create2"); exit(1); } pthread_join(tid[0],NULL); pthread_join(tid[1],NULL); close(sockfd); return 0; }

다중 스레드로 다음과 같은 기능을 수행할 수 있습니다.
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT   8888

struct info 
{
    int ToFd;
    char buf[100];
};

void *MyReceive(void *arg)
{
    struct info RecvBuf = {0};
    int ret;

    pthread_detach(pthread_self());

    while (1)
    {
        ret = recv(*(int *)arg, &RecvBuf, sizeof(RecvBuf), 0);
        if (-1 == ret)
        {
            perror("recv");
            exit(1);
        }
        if (!strcmp(RecvBuf.buf, "bye"))
        {
            close(*(int *)arg);
            break;
        }
        //printf("Recv From Client %d : %s
", *(int *)arg, buf);
ret = send(RecvBuf.ToFd, &RecvBuf, sizeof(RecvBuf), 0); if (-1 == ret) { perror("send"); exit(1); } memset(&RecvBuf, 0, sizeof(RecvBuf)); } } int main() { int sockfd, ret, fd[100] = {0}, length, i = 0; char buf[100] = {0}; struct sockaddr_in server_addr; struct sockaddr_in client_addr; pthread_t tid[100] = {0}; printf("Start Server!
"
); sockfd = socket(PF_INET, SOCK_STREAM, 0); if (-1 == sockfd) { perror("socket"); exit(1); } int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = PF_INET; server_addr.sin_port = PORT; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (-1 == ret) { perror("bind"); exit(1); } printf("Listening...
"
); ret = listen(sockfd, 5); if(-1 == ret) { perror("listen"); exit(1); } while (1) { length = sizeof(client_addr); fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length); if (-1 == fd[i]) { perror("accept"); exit(1); } printf("Accept %d , Port %d
"
, fd[i], client_addr.sin_port); ret = pthread_create(&tid[i], NULL, MyReceive, (void *)&fd[i]); if (0 != ret) { perror("pthread_create"); exit(1); } i++; } close(sockfd); return 0; }
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT    8888

struct info
{
    int ToFd;
    char buf[100];
};

pthread_t tid[2] = {0};

void *Send(void *arg)
{
    struct info SendBuf = {0};
    int ret;

    while (1)
    {
        scanf("%s %d", SendBuf.buf, &SendBuf.ToFd);

        ret = send(*(int *)arg, &SendBuf, sizeof(SendBuf), 0);
        if (-1 == ret)
        {
            perror("send");
            exit(1);
        }

        if (!strcmp(SendBuf.buf, "bye"))
        {
            close(*(int *)arg);
            pthread_cancel(tid[1]);
            break;
        }

        memset(&SendBuf, 0, sizeof(SendBuf));
    }

}

void *Recv(void *arg)
{
    struct info RecvBuf = {0};
    int ret, oldtype;

    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);

    while (1)
    {
        ret = recv(*(int *)arg, &RecvBuf, sizeof(RecvBuf), 0);
        if (-1 == ret)
        {
            perror("recv");
            exit(1);
        }

        printf("\t\t\t%s
"
, RecvBuf.buf); memset(&RecvBuf, 0, sizeof(RecvBuf)); } } int main() { int sockfd, ret; struct sockaddr_in server_addr; sockfd = socket(PF_INET, SOCK_STREAM, 0); if (-1 == sockfd) { perror("socket"); exit(1); } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = PF_INET; server_addr.sin_port = PORT; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); printf("Start Connecting...
"
); ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (-1 == ret) { perror("connect"); exit(1); } ret = pthread_create(&tid[0], NULL, Send, (void *)&sockfd); if (0 != ret) { perror("pthread_create1"); exit(1); } ret = pthread_create(&tid[1], NULL, Recv, (void *)&sockfd); if (0 != ret) { perror("pthread_create2"); exit(1); } pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); close(sockfd); return 0; }

위의 프로그램 수정 버전:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT   8888

struct info 
{
    int ToFd;
    char buf[100];
};


int main()
{
    int sockfd, ret, fd[100] = {0}, length, i = 0, j;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    fd_set ReadFd, TmpFd;
    int MaxFd;
    struct info RecvBuf;

    printf("Start Server!
"
); sockfd = socket(PF_INET, SOCK_STREAM, 0); if (-1 == sockfd) { perror("socket"); exit(1); } int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = PF_INET; server_addr.sin_port =htons(PORT); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (-1 == ret) { perror("bind"); exit(1); } ret = listen(sockfd, 5); if(-1 == ret) { perror("listen"); exit(1); } FD_ZERO(&ReadFd); FD_ZERO(&TmpFd); FD_SET(sockfd,&ReadFd); MaxFd = sockfd; while(1) { TmpFd = ReadFd; ret = select(MaxFd + 1, &TmpFd, NULL, NULL, NULL); if(-1 == ret) { perror("select"); exit(1); } if(FD_ISSET(sockfd,&TmpFd)) { length = sizeof(client_addr); fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length); if (-1 == fd[i]) { perror("accept"); exit(1); } printf("Accept %d , Port %d
"
, fd[i], client_addr.sin_port); FD_SET(fd[i],&ReadFd); MaxFd = fd[i]; i++; } else { for(j = 0; j < i; i++) { if(FD_ISSET(fd[j],&TmpFd)) { ret = recv(fd[j],&RecvBuf,sizeof(RecvBuf),0); if(ret < 0) { perror("recv"); exit(1); } ret = send(RecvBuf.ToFd,&RecvBuf,sizeof(RecvBuf),0); if(ret < 0) { perror("send"); exit(1); } } } } } return 0; }
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT    8888

struct info
{
    int ToFd;
    char buf[100];
};

pthread_t tid[2] = {0};

void *Send(void *arg)
{
    struct info SendBuf = {0};
    int ret;

    while (1)
    {
        scanf("%s %d", SendBuf.buf, &SendBuf.ToFd);

        ret = send(*(int *)arg, &SendBuf, sizeof(SendBuf), 0);
        if (-1 == ret)
        {
            perror("send");
            exit(1);
        }

        if (!strcmp(SendBuf.buf, "bye"))
        {
            close(*(int *)arg);
            pthread_cancel(tid[1]);
            break;
        }

        memset(&SendBuf, 0, sizeof(SendBuf));
    }

}

void *Recv(void *arg)
{
    struct info RecvBuf = {0};
    int ret, oldtype;

    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);

    while (1)
    {
        ret = recv(*(int *)arg, &RecvBuf, sizeof(RecvBuf), 0);
        if (-1 == ret)
        {
            perror("recv");
            exit(1);
        }

        printf("\t\t\t%s
"
, RecvBuf.buf); memset(&RecvBuf, 0, sizeof(RecvBuf)); } } int main() { int sockfd, ret; struct sockaddr_in server_addr; sockfd = socket(PF_INET, SOCK_STREAM, 0); if (-1 == sockfd) { perror("socket"); exit(1); } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = PF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); printf("Start Connecting...
"
); ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (-1 == ret) { perror("connect"); exit(1); } ret = pthread_create(&tid[0], NULL, Send, (void *)&sockfd); if (0 != ret) { perror("pthread_create1"); exit(1); } ret = pthread_create(&tid[1], NULL, Recv, (void *)&sockfd); if (0 != ret) { perror("pthread_create2"); exit(1); } pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); close(sockfd); return 0; }

좋은 웹페이지 즐겨찾기