c双LAN插座编程

时间:2015-06-30 04:27:21

标签: c sockets

int  udp_sock()
      {
    size_t size=sizeof(server);

               //Create socket
                  sock = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP);


                  if (sock == -1)
                  {
                      printf("Could not create socket\n");
                  }
                  puts("Socket created.......\n");


                  server.sin_addr.s_addr = inet_addr("192.168.210.9");
                  server.sin_family = AF_INET;
                  server.sin_port = htons(4000);
                             // Connect to remote server
              client.sin_addr.s_addr = inet_addr("192.168.210.48");
                                      client.sin_family = AF_INET;
                                      client.sin_port = htons(4000);

                  if(bind(sock, (struct sockaddr *) &client, sizeof(client)) < 0) {
                                          perror("bind failed\n");
                  }
                  puts("Connected\n");

                  n=sendto(sock,buffer,strlen(buffer),0,(struct sockaddr *)&server,size);
                  return 0;
      }

int  udp_sock1()
      {
    size_t size=sizeof(server);

               //Create socket
                  sock = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP);


                  if (sock == -1)
                  {
                      printf("Could not create socket\n");
                  }
                  puts("Socket created.......\n");

                  memset(&server, 0, sizeof(server));
                  server.sin_addr.s_addr = inet_addr("192.168.210.60");
                  server.sin_family = AF_INET;
                  server.sin_port = htons(5000);
                  client.sin_addr.s_addr = inet_addr("192.168.210.48");
                                                      client.sin_family = AF_INET;
                                                      client.sin_port = htons(4000);

                                  if(bind(sock, (struct sockaddr *) &client, sizeof(client)) < 0) {
                                                          perror("bind failed\n");
                                  }
                                  puts("Connected\n");
                                rc=recvfrom(sock, buffer,1024 , 0,(struct sockaddr *)&client,&size);

                                  return 0;
      }

1.我正在使用双LAN PC。我必须接收上文192.168.210.60 port no. 5000中定义的udp_sock1()数据。 并将收到的数据发送到192.168.210.9 with port 4000中定义的udp_sock。 我的系统IP是192.168.210.48 port 4000

plz指南。

0 个答案:

没有答案