zoukankan      html  css  js  c++  java
  • 【网络编程】——windows socket 编程

    测试demo

     1 #include <winsock2.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #if 0
     6 #define TCP
     7 #else
     8 #define UDP
     9 #endif
    10 
    11 #pragma comment(lib, "ws2_32.lib")
    12 char *ip = "10.8.2.60";
    13 #ifdef UDP
    14 short port = 1025;
    15 #endif
    16 #ifdef TCP
    17 short port = 2222;
    18 #endif
    19 SOCKET fd;
    20 
    21 int init(void) {
    22     WSADATA wsaData;
    23     int nRet;
    24     if((nRet = WSAStartup(MAKEWORD(2,2),&wsaData)) != 0){
    25         printf("WSAStartup failed!
    ");
    26         exit(0);
    27     }
    28 }
    29 
    30 int init_socket(void){
    31     SOCKADDR_IN local;
    32 #ifdef UDP
    33     if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
    34 #endif
    35 #ifdef TCP
    36     if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1){
    37 #endif
    38         printf("%d
    ", WSAGetLastError());
    39         perror("socket");
    40         //getchar();
    41         return -1;
    42     }
    43 
    44     printf("fd:%d start......
    ", fd);
    45 
    46     local.sin_family = AF_INET;
    47     local.sin_port = htons((short)port);
    48     local.sin_addr.s_addr = inet_addr(ip);
    49     //local.sin_addr.S_un.S_addr = inet_addr(ip);
    50 
    51     if (bind(fd, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR) {
    52         printf("reason %d ", WSAGetLastError());
    53         printf("bind failed!
    ");
    54         getchar();
    55         return -1;
    56     }
    57 
    58 #ifdef TCP
    59     if (listen(fd, 10) == -1){
    60         perror("listen");
    61         return -1;
    62     }
    63 #endif
    64     return 0;
    65 }
    66 
    67 int main(int argc, char *argv[]) {
    68     char buffer[2048];
    69     int size;
    70     SOCKADDR_IN from;
    71     int addrlen = sizeof(from);
    72     SOCKET client;
    73     unsigned int index = 0;
    74 
    75     init();
    76     if (init_socket()) {
    77         //printf("init socket error!
    ");
    78         return -1;
    79     }
    80 
    81     while (1){
    82         index++;
    83 #ifdef UDP
    84         size = recvfrom(fd, buffer, sizeof(buffer), 0, NULL, NULL);
    85         printf("recvfrom:%d index:%u
    ", size, index);
    86 #endif
    87 #ifdef TCP
    88         if ((client = accept(fd, (SOCKADDR *)&from, &addrlen)) == -1){
    89             perror("accept");
    90             return -1;
    91         }
    92         size = recv(client, buffer, sizeof(buffer), 0);
    93         printf("recv:%d
    ", size);
    94 #endif
    95     }
    96 
    97     getchar();
    98     return 0;
    99 }

     以上是 server 端,一下是 client 端:

     1 #include <winsock2.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 
     6 #if 0
     7 #define UDP
     8 #else
     9 #define TCP
    10 #endif
    11 
    12 #pragma comment(lib, "ws2_32.lib")
    13 char *ip = "10.8.2.56";
    14 
    15 #ifdef UDP
    16 short port = 1025;
    17 #else
    18 short port = 2222;
    19 #endif
    20 
    21 SOCKET fd;
    22 
    23 int init(void) {
    24     WSADATA wsaData;
    25     int nRet;
    26     if((nRet = WSAStartup(MAKEWORD(2,2),&wsaData)) != 0){
    27         printf("WSAStartup failed!
    ");
    28         exit(0);
    29     }
    30 }
    31 
    32 int init_socket(void){
    33 
    34 
    35 #ifdef UDP
    36     if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
    37 #else
    38     SOCKADDR_IN local;
    39     if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1){
    40 #endif
    41         printf("%d
    ", WSAGetLastError());
    42         perror("socket");
    43         //getchar();
    44         return -1;
    45     }
    46 
    47 #ifdef TCP
    48     
    49     local.sin_family = AF_INET;
    50     local.sin_port = htons(port);
    51     local.sin_addr.S_un.S_addr = inet_addr(ip);
    52 
    53     if (connect(fd, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR) {
    54         printf("connect error!
    ");
    55         perror("connect");
    56         return -1;
    57     }
    58 #endif
    59     printf("fd:%d start......
    ", fd);
    60 
    61     return 0;
    62 }
    63 
    64 int main(int argc, char *argv[]) {
    65     char buffer[512];
    66     int size;
    67     int result = 0;
    68     unsigned int index = 0;
    69 
    70     init();
    71 
    72     if (init_socket()){
    73         return -1;
    74     }
    75 
    76 #ifdef UDP
    77     SOCKADDR_IN sin;
    78     int addrlen = sizeof(sin);
    79     sin.sin_family = AF_INET;
    80     sin.sin_port = htons(port);
    81     sin.sin_addr.s_addr = inet_addr(ip);
    82 #endif
    83 
    84     memset(buffer, 1, sizeof(buffer));
    85     while (1) {
    86 #ifdef UDP
    87         result = sendto(fd, buffer, sizeof(buffer), 0, (SOCKADDR *)&sin, addrlen);
    88 #else
    89         result = send(fd, buffer, sizeof(buffer), 0);
    90 #endif
    91         printf("result:%d, index:%d
    ", result, index);
    92         getchar();
    93         index++;
    94     }
    95     
    96     return 0;
    97 }
  • 相关阅读:
    [置顶] 深入理解android之IPC机制与Binder框架
    cdn加速对门户网站产生的影响
    极客技术专题【007期】:jQuery初学者入门
    linux 下信号处理命令trap && linux下各种信号的意义
    利用ACE 自己实现的线程池
    漫谈开发前奏之编译器
    jar,war,ear区别及java基础杂七八
    java出现no XXX in java.library.path的解决办法及eclipse配置
    Windows下GNU之gcc体验方法
    防asp木马运行
  • 原文地址:https://www.cnblogs.com/ngnetboy/p/5555408.html
Copyright © 2011-2022 走看看