码迷,mamicode.com
首页 > 系统相关 > 详细

【网络编程】——linux socket demo

时间:2016-06-17 21:13:43      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

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

#if 0
#define UDP
#else
#define TCP
#endif

int sockfd;
char* IP = "10.8.2.60";
//char *IP = "127.0.0.1";
#ifdef UDP
short PORT = 1025;
#endif
#ifdef TCP
short PORT = 2222;
#endif
typedef struct sockaddr SA;

void init(){
#ifdef UDP
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
#endif

#ifdef TCP
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);
    if(connect(sockfd, (SA*)&addr, sizeof(addr)) == -1){
        printf("connect failed!\n");
        exit(-1);
    }
#endif
}

int main (int argc, char *argv[]) {
    //char buf[2048];
    char buf[512];
    int index, i = 0;

    if (argc == 2) {
        index = atoi(argv[1]);
    } else {
        printf("Usage: ./client <times>\n");
        return -1; 
    } 

    printf("start init ....\n");
    init();
    printf("connected...\n");
    memset(buf, 1, sizeof(buf));
#ifdef UDP
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);
#endif

    while (1) {
        //printf("please input something:\n");
        //scanf("%s", buf);
        //puts(buf);
        if (i == index)
            break;
#ifdef TCP
        send(sockfd, buf, sizeof(buf), 0);
        printf("send 2048! index:%d\n", i);
#endif
#ifdef UDP
        printf("sendto 2048! index:%d\n", i);
        if (sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1){
            printf("sendto error!\n");
            return -1;
        }
#endif
        //sleep(1);
        i++;
    }

    close(sockfd);

    return 0;
}

client。

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

int sockfd;
char* IP = "10.8.2.56";
//char *IP = "127.0.0.1";
typedef struct sockaddr SA;

#if 0
#define UDP
#else
#define TCP
#endif
#ifdef UDP
short PORT = 1025;
#else
short PORT = 2222;
#endif

void init(){
#ifdef TCP
    sockfd = socket(AF_INET, SOCK_STREAM, 0);    //tcp
#endif
#ifdef UDP
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);    //udp
#endif
    if(sockfd == -1){
        printf("socket failed!\n");
        exit(-1);
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);
    if(bind(sockfd, (SA *)&addr, sizeof(addr)) == -1) {
        printf("bind failed!\n");
        exit(-1);
    }
#ifdef TCP
    if(listen(sockfd, 10) == -1) {
        printf("listen failed!\n");
        exit(-1);
    }
#endif
}

int main (int argc, char *argv[]) {
    int fd;
    int ret;
    char buf[512];
    int index = 0;
    init();

    while (1) {
#ifdef TCP
        struct sockaddr_in fromaddr;
        socklen_t len = sizeof(fromaddr);
        fd = accept(sockfd, (SA *)&fromaddr, &len);
        if (fd < 0) {
            printf("accept failed!\n");
            continue;
        }
        printf("fd:%d\n", fd);
#endif
        memset(buf, 1, sizeof(buf));
        while (1) {
#ifdef TCP
            if ((ret = recv(fd, buf, sizeof(buf), MSG_WAITALL)) > 0) {
#endif
#ifdef UDP
            printf("udp!\n");
            if ((ret = recvfrom(sockfd, buf, sizeof(buf), MSG_WAITALL, NULL, NULL)) > 0) {
#endif
                printf("ret %d\n", ret);
            } else {
                printf("recvfrom failed! ret:%d\n", ret);
                break;
            }
            printf("index:%d\n", index);
            index++;
        }
    }

    return 0;
}

server。

【网络编程】——linux socket demo

标签:

原文地址:http://www.cnblogs.com/ngnetboy/p/5594971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!