无法从两个不同的套接字发送数据

时间:2014-08-05 08:53:14

标签: c++ c sockets winapi networking

我正在使用win32api来操作套接字。 我想要做的是一个简单的服务器,它有两个线程 - 每个线程创建和初始化一个套接字。一个简单的客户端应该连接(再次,使用2个不同的套接字)到服务器。然后,服务器应该从2个套接字发送2条消息,客户端应该在屏幕上打印它们。

服务器代码:

#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>

DWORD socket1_listen (void);
DWORD socket2_listen (void);

int main(int argc , char *argv[])
{
    WSADATA wsa;
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d\n",WSAGetLastError());
        return 1;
    }

    DWORD ThreadId1,ThreadId2;
    HANDLE thread1 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)socket1_listen,NULL,0,&ThreadId1); 
    HANDLE thread2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)socket2_listen,NULL,0,&ThreadId2);

    Sleep(40*1000);
    return 0;
}

DWORD socket1_listen (void){

    struct sockaddr_in server2;
    SOCKET socket2 , new_socket;

    server2.sin_family = AF_INET;
    server2.sin_addr.s_addr = INADDR_ANY;
    server2.sin_port = htons( 8888 );

  if((socket2 = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d\n" , WSAGetLastError());
    }

    int bOptVal = 1;
    int bOptLen = sizeof (int);
    setsockopt(socket2, SOL_SOCKET, SO_REUSEADDR, (char *) &bOptVal, bOptLen);

    if( bind(socket2 ,(struct sockaddr *)&server2 , sizeof(server2)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
    }

    listen (socket2, 1000);
    new_socket=accept (socket2 , NULL , NULL);
    printf ("accepted: socket 2");
    char msg2[] = "hi there , this is socket 2";
    send (new_socket, msg2,strlen(msg2),0);

}

DWORD socket2_listen (void){

    SOCKET socket1 , new_socket;
    struct sockaddr_in server1;

 //Create a socket
    if((socket1 = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d\n" , WSAGetLastError());
    }

    printf("Sockets created.\n");

    //Prepare the sockaddr_in structure
    server1.sin_family = AF_INET;
    server1.sin_addr.s_addr = INADDR_ANY;
    server1.sin_port = htons( 8888 );

    int bOptVal = 1;
    int bOptLen = sizeof (int);

    setsockopt(socket1, SOL_SOCKET, SO_REUSEADDR, (char *) &bOptVal, bOptLen);

    //Bind
    if( bind(socket1 ,(struct sockaddr *)&server1 , sizeof(server1)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
    }

    listen(socket1 , 1000);
    new_socket=accept (socket1 , NULL , NULL);
    printf ("accepted: socket 1");     

    char msg[] = "hi there, this is socket1\n";

    send(new_socket,msg,strlen(msg),0);     

}

客户端代码:

#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>

int main(int argc, char** argv) {

    WSADATA wsadata;
    WSAStartup(MAKEWORD(2,2),&wsadata);

    SOCKET socket1 , socket2;
    struct sockaddr_in address1 , address2;

    socket1=socket(AF_INET,SOCK_STREAM,0);
    socket2=socket(AF_INET,SOCK_STREAM,0);


    address1.sin_addr.s_addr=inet_addr("<my IP>");
    address1.sin_family=AF_INET;
    address1.sin_port=htons(8888);


    address2.sin_addr.s_addr=inet_addr("<my IP>");
    address2.sin_family=AF_INET;
    address2.sin_port=htons(8888);

    int is_connected_s,is_connected_r;

    is_connected_s=connect(socket1,(struct sockaddr*)&address1,sizeof(address1));
    is_connected_r=connect(socket2,(struct sockaddr*)&address2,sizeof(address2));

    std::cout<<"status for socket 1: "<<is_connected_s<<"\n";
    std::cout<<"status for socket 2: "<<is_connected_r<<"\n";

    char buf[1000];
    recv(socket1,buf,999,0);
    buf[999]=0;
    std::cout<<buf<<"\n"<<"*******"<<"\n";  

    strcpy(buf,"");

    recv(socket2,buf,999,0);
    buf[999]=0;
    std::cout<<buf<<"\n"<<"*******"<<"\n";  

    return 0;
} 

0 个答案:

没有答案