Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1204580
  • 博文数量: 252
  • 博客积分: 5421
  • 博客等级: 大校
  • 技术积分: 2418
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-17 12:59
文章分类

全部博文(252)

文章存档

2017年(3)

2016年(18)

2015年(31)

2014年(18)

2013年(7)

2012年(8)

2011年(12)

2010年(30)

2009年(32)

2008年(57)

2007年(36)

分类: C/C++

2010-08-21 21:25:36

服务端代码:

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

#define MAXDATASIZE 1024

#define SERVERPORT 8000
#define MAXCONN_NUM 10

int main()
{
    char buf[MAXDATASIZE];
    int sockfd, new_fd, numbytes;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    int sin_size;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket error");
        return 1;
    }
    memset(&client_addr, 0, sizeof(struct sockaddr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVERPORT);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    if (bind(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) == -1) {
        perror("bind error");
        return 1;
    }
    if (listen(sockfd, MAXCONN_NUM) == -1) {
        perror("listen error");
        return 1;
    }

    while (1) {
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size)) == -1) {
            perror("accept error");
            continue;
        }
        printf("server: got connection from %s\n", inet_ntoa(client_addr.sin_addr));
        if ((numbytes = recv(new_fd, buf, MAXDATASIZE, 0)) == -1) {
            perror("recv error");
            return 1;
        }
        if (numbytes) {
            buf[numbytes] = '\0';
            printf("received: %s\n", buf);
            sleep(3);
        }
        printf("send: hi~~\n");
        if (send(new_fd, "hi~~", 5, 0) == -1) {
            perror("send error");
            return 1;
        }
        close(new_fd);
    }
    return 0;
}




客户端代码:

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

#define MAXDATASIZE 1024

#define SERVERIP "192.168.0.2"
#define SERVERPORT 8000

int main(int argc, char *argv[])
{
    char buf[MAXDATASIZE];
    int sockfd, numbytes;
    struct sockaddr_in server_addr;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket error");
        return 1;
    }
    memset(&server_addr, 0, sizeof(struct sockaddr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVERPORT);
    server_addr.sin_addr.s_addr = inet_addr(SERVERIP);
    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        perror("connect error");
        return 1;
    }
    printf("send: Hello, world!\n");
    if (send(sockfd, "Hello, world!", 14, 0) == -1) {
        perror("send error");
        return 1;
    }
    if ((numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
        perror("recv error");
        return 1;
    }
    if (numbytes) {
        buf[numbytes] = '\0';
        printf("received: %s\n", buf);
    }
    close(sockfd);
    return 0;
}


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