socket编程实例

server.c
#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>

#define PORT 5555
#define MAXNSG 512

int make_socket(uint16_t port)
{
    int sock;
    struct sockaddr_in name;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock < 0)
    {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    name.sin_family = AF_INET;
    name.sin_port = htons(port);
    name.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0)
    {
        perror("bind");
        exit(EXIT_FAILURE);
    }

    return sock;
}

int read_from_client(int filedes)
{
    char buffer[MAXNSG];
    int nbytes;

    nbytes = read(filedes, buffer, MAXNSG);
    if(nbytes < 0)
    {
        perror("read");
        exit(EXIT_FAILURE);
    }
    else if(nbytes == 0)
    {
        return -1;
    }
    else
    {
        fprintf(stderr, "Server: got message: %s\n", buffer);
        return 0;
    }
}

int main(int argc, char** argv)
{
    int sock;
    fd_set active_fd_set, read_fd_set;
    int i;
    struct sockaddr_in clientname;
    size_t size;

    sock = make_socket(PORT);
    if(listen(sock,1) < 0)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    FD_ZERO(&active_fd_set);
    FD_SET(sock, &active_fd_set);

    while(1)
    {
        read_fd_set = active_fd_set;
        if(select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
        {
            perror("select");
            exit(EXIT_FAILURE);
        }

        for(i = 0;i < FD_SETSIZE; ++i)
        {
            if (FD_ISSET(i,&read_fd_set))
            {
                if(i == sock)
                {
                    int new;
                    size = sizeof(clientname);
                    new = accept(sock, (struct sockaddr *) &clientname,(socklen_t *) &size);
                    if(new < 0)
                    {
                        perror("accept");
                        exit(EXIT_FAILURE);
                    }

                    fprintf(stderr,
                            "Server: connect from host %s, port %hd.\n",
                            inet_ntoa(clientname.sin_addr),
                            ntohs(clientname.sin_port));
                    FD_SET(new, &active_fd_set);
                }
                else
                {
                    if(read_from_client(i) < 0)
                    {
                        close(i);
                        FD_CLR(i, &active_fd_set);
                    }
                }
            }
        }
    }
}
client.c
#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>

#define PORT 5555
#define MESSAGE "Yow!!! Are we having fun yet?!?"
#define SERVERHOST "localhost"

void write_to_server(int filedes)
{
    int nbytes;
    nbytes = write(filedes, MESSAGE, strlen(MESSAGE) + 1);
    if (nbytes < 0)
    {
        perror("write");
        exit(EXIT_FAILURE);
    }
}

void init_sockaddr(struct sockaddr_in* name, const char* hostname, uint16_t port)
{
    struct hostent* hostinfo;

    name->sin_family = AF_INET;
    name->sin_port = htons(port);
    hostinfo = gethostbyname(hostname);

    if (hostinfo == NULL)
    {
        fprintf(stderr, "Unknown host %s.\n",hostname);
        exit(EXIT_FAILURE);
    }

    name->sin_addr = *(struct in_addr*)hostinfo->h_addr;
}

int main(int argc,char** argv)
{

    int sock;
    struct sockaddr_in servername;

    sock = socket(PF_INET, SOCK_STREAM, 0);

    if (sock < 0)
    {
        perror("socket (client)");
        exit(EXIT_FAILURE);
    }

    init_sockaddr(&servername, SERVERHOST, PORT);

    if (0 > connect(sock, (struct sockaddr *) &servername, sizeof(servername)))
    {
        perror("connect (client))");
        exit(EXIT_FAILURE);
    }

    write_to_server(sock);
    close(sock);
    exit(EXIT_SUCCESS);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值