C/S模式简单socket通信

TCP连接方式

sever.c

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

#define PORT 8000
#define QUEN 20
#define MAXLINE 2048
int main()
{
    int sock_fd,connect_fd;
    char BUFFER[2048];
    int n;
    struct sockaddr_in severaddr;
    if((sock_fd = socket(AF_INET, SOCK_STREAM, 0))==-1)
    {
        printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
        exit(0);
    }
    memset(&severaddr,0,sizeof(severaddr));
    severaddr.sin_family = AF_INET;
    severaddr.sin_port = htons(PORT);
    severaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    if((bind(sock_fd,(struct sockaddr*)&severaddr, sizeof(severaddr)))==-1)
    {
        printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
        exit(0);
    }
    if(listen(sock_fd,QUEN)==-1)
    {
        printf("listen error");
    }
    printf("=============wating for client=========================\n");

    connect_fd = accept(sock_fd,(struct sockaddr*)NULL, NULL);
    while((n = recv(connect_fd,BUFFER,MAXLINE,0))>0)
    {
        if(!fork())  //son
        {
            if((send(connect_fd,BUFFER,sizeof(BUFFER),0)) == -1)
                perror("send error");
            BUFFER[n]='\0';
            printf("receive message from client:  %s\n",BUFFER);
            close(connect_fd);
            exit(0);
        }
        if((strcmp(BUFFER,"exit"))==0)
        {
            printf("exit link");
            break;
        }
    }

    close(connect_fd);
    close(sock_fd);

}
                 

 

 

 

client.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

#define MAXLINE 2048
#define PORT 8000
int main()
{
    int sock_fd,rec_len;
    char recvline[2048],sendline[2048];
    char buf[MAXLINE];
    struct sockaddr_in clientaddr;


    if((sock_fd = socket(AF_INET, SOCK_STREAM,0))==-1)
    {
        printf("socket link error\n");
    }
    memset(&clientaddr,0,sizeof(clientaddr));
    clientaddr.sin_family = AF_INET;
    clientaddr.sin_port =htons( PORT);
    clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if((connect(sock_fd,(struct sockaddr*)&clientaddr,sizeof(clientaddr)))==-1)
    {
        printf("connect errot\n");
        exit(0);
    }
    while(fgets(sendline,2048,stdin) != NULL)
    {

        printf("send message to sever:%s\n",sendline);

        send(sock_fd,sendline, sizeof(sendline),0);

        rec_len = recv(sock_fd,buf,MAXLINE,0);
        if(sendline=="exit")
        {
            printf("exit link\n");
            break;
        }
        buf[rec_len] = '\0';
        printf("receive:%s\n",buf);


    }

    close(sock_fd);

    exit(0);
}

 

 

 

UDP方式连接

sever.c

#include <stdio.h>

#include <sys/socket.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <stdlib.h>

#define FILE_NAME_SIZE 512

#define PORT 8000

#define BUFFERSIZE 1024

int main()

{

        int sock_id, bind_id;

        struct sockaddr_in severaddr;

        if((sock_id = socket(AF_INET,SOCK_DGRAM,0)) == -1)

        {

                printf("socket error");

                exit(0);

        }

        memset(&severaddr,0, sizeof(severaddr));

        severaddr.sin_family = AF_INET;

        severaddr.sin_port = htons(PORT);

        severaddr.sin_addr.s_addr = htonl(INADDR_ANY);

        if((bind_id = bind(sock_id, (struct sockaddr*)&severaddr, sizeof(severaddr))) == -1)

        {                 printf("bind error");                 exit(0);         }

        while(1)

        {

                struct sockaddr_in clientaddr;

                socklen_t clientaddr_length = sizeof(clientaddr);

                char recvbuf[BUFFERSIZE];

                bzero(recvbuf, BUFFERSIZE);

                if((recvfrom(sock_id,recvbuf,BUFFERSIZE,0,(struct sockaddr*)&clientaddr,&clientaddr_length))==-1)

                {                         printf("recv error");                         exit(0);                 }

                char file_name[FILE_NAME_SIZE];

                bzero(file_name, FILE_NAME_SIZE);

                strncpy(file_name,recvbuf,strlen(recvbuf)>FILE_NAME_SIZE?FILE_NAME_SIZE:strlen(recvbuf));
                printf("%s\n",file_name);
        }
        close(sock_id);
        return 0;
}

 

 

 

client.c

#include <stdio.h>

#include <stdlib.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <string.h>

#include <errno.h>

#define PORT 8000

#define BUFFER_SIZE 1024

int main()

{

        int sock_id;

        struct sockaddr_in clientaddr;

        if((sock_id = socket(AF_INET, SOCK_DGRAM, 0)) == -1)

        {                 perror("sock error");                 exit(1);         }

        memset(&clientaddr, 0, sizeof(clientaddr));

        clientaddr.sin_family = AF_INET;

        clientaddr.sin_port   = htons(PORT);

        clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

        char file_name[BUFFER_SIZE];

        bzero(file_name, BUFFER_SIZE);

        scanf("%s\n",file_name);

        if(sendto(sock_id, file_name, BUFFER_SIZE, 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr)) <0)

        {                 perror("send error");                 exit(1);         }

        close(sock_id);

        return 0;

}

 

posted @ 2017-07-12 10:00  Edver  阅读(815)  评论(0编辑  收藏  举报