Chinaunix首页 | 论坛 | 博客
  • 博客访问: 349078
  • 博文数量: 60
  • 博客积分: 1570
  • 博客等级: 上尉
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-02 23:37
文章分类

全部博文(60)

文章存档

2012年(2)

2010年(2)

2009年(56)

分类: LINUX

2009-11-08 22:59:58

服务器端代码:

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

#define PORT 6666
#define LISTEN_QUEUE 200

#define errexit(msg) do{perror(msg); exit(EXIT_FAILURE);} while (0)

int main(int argc, char *argv[])
{
    int listenfd, clt_fd;
    int on = 1;
    
    pid_t pid;

    struct sockaddr_in srv_addr, clt_addr;
    int addrlen;

    int len;
    char recvbuf[1024]; /* you'd better use malloc function to allocate memory for it */

    if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        errexit("socket error");

    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
        perror("warning: setsockopt reuse addr failed!");

    addrlen = sizeof(struct sockaddr);
    memset(&srv_addr, 0, addrlen);
    srv_addr.sin_family = AF_INET;
    srv_addr.sin_port = htons(PORT);
    srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(listenfd, (struct sockaddr *)&srv_addr, addrlen) < 0) {
        close(listenfd);
        errexit("bind error");
    }

    if (listen(listenfd, LISTEN_QUEUE) < 0) {
        close(listenfd);
        errexit("listen error");
    }

    signal(SIGCHLD, SIG_IGN);

    printf("waiting for accepting connection...\n");
    while (1) {
        clt_fd = accept(listenfd, (struct sockaddr *)&clt_addr, &addrlen);
        if (clt_fd < 0) {
            if (errno == EINTR)
                continue;
            else {
                perror("cannot accept client connect request!");
                close(listenfd);
                exit(1);
            }
        }

        if ((pid = fork()) < 0) {
            perror("fork error");
            close(listenfd);
            close(clt_fd);
            exit(1);
        } else if (pid == 0) {
            memset(recvbuf, 0, sizeof(recvbuf));
            while ((len = recv(clt_fd, recvbuf, sizeof(recvbuf), 0)) > 0) {
                printf("Message from client(%d): %s\n", len, recvbuf);
                if (recvbuf[0] == '@')
                    break;
                int i;
                for (i=0; i<len; i++)
                    recvbuf[i] = toupper(recvbuf[i]);
                send(clt_fd, recvbuf, sizeof(recvbuf), 0);
                memset(recvbuf, 0, sizeof(recvbuf));
            }
            close(clt_fd);
            close(listenfd);
            _exit(0);
        } else {
            close(clt_fd);
        }
    }

    exit(0);
}


 

 

客户端代码:

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

#define IP "127.0.0.1"
#define PORT 6666

#define errexit(msg) do{perror(msg); exit(EXIT_FAILURE);} while (0)

int main(int argc, char *argv[])
{
    int sockfd;
    
    int addrlen;
    struct sockaddr_in srv_addr;

    int len;
    char sendbuf[1024]; /* you'd better use malloc function to allocate memory for it */

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        errexit("socket error");
    
    addrlen = sizeof(struct sockaddr_in);
    memset(&srv_addr, 0, addrlen);
    srv_addr.sin_family = AF_INET;
    srv_addr.sin_port = htons(PORT);
    srv_addr.sin_addr.s_addr = inet_addr(IP);

    if (connect(sockfd, (struct sockaddr *)&srv_addr, addrlen) < 0) {
        close(sockfd);
        errexit("connect error");
    }

    memset(sendbuf, 0, sizeof(sendbuf));
    char *prompt = "input message: ";
    while (1) {
        memset(sendbuf, 0, sizeof(sendbuf));
        
        write(STDOUT_FILENO, prompt, strlen(prompt));
        len = read(STDIN_FILENO, sendbuf, sizeof(sendbuf));
        if (sendbuf[0] == '@')
            break;
        if (len > 0) 

            /* 注意,在这里,由于 send 函数是把数据发送到自己机器的socket的发送缓冲区,然后返回,(http://blog.chinaunix.net/u1/35065/showart_1950572.html )

所以如果服务器端连接成功,然后在 send 之前关闭,这里 send 函数虽然成功返回,但是并不表示数据已经被对方接收到了 */
            if (send(sockfd, sendbuf, len, 0) == -1) {
                perror("send error");    
                break;
            }
        
        memset(sendbuf, 0, sizeof(sendbuf));
        len = recv(sockfd, sendbuf, sizeof(sendbuf), 0);
        if (len > 0)
            printf("Message from server: %s\n", sendbuf);
        else if (len < 0){
            perror("recv error");    
            break;
        } else { /* 如果recv 函数返回0, 表示对方已经关闭了socket 连接*/
            printf("the peer has performed an orderly shutdown.\n");
            break;
        }
    }

    close(sockfd);
    exit(0);
}


阅读(1262) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~