简体   繁体   English

Winsock绑定在同一端口上

[英]Winsock binding on the same port

So I recently decided to dabble into winsock and network programming a bit by using a guide I found and searching the net, but I have run into a problem which I am not really sure how i should solve. 因此,我最近决定通过使用我发现的指南并在网上搜索来涉足Winsock和网络编程,但遇到了一个我不确定如何解决的问题。

I'm trying to make a very simple chat system, I've got a working server program and client program, and if I only use on client (sending the message back to the same client) It seems to work perfectly fine. 我正在尝试创建一个非常简单的聊天系统,我有一个可以正常工作的服务器程序和客户端程序,并且如果我仅在客户端上使用(将消息发送回同一客户端),它似乎可以正常工作。 The problem appears when I try to have multiple clients connect. 当我尝试连接多个客户端时出现问题。 I get error 10048 from WSAgetlasterror and it seems to be the bind function that is the source, more specificly the fact that I am trying to bind on the same port twice, (once for each client). 我从WSAgetlasterror收到错误10048,它似乎是源的绑定函数,更具体地说,我试图在同一端口上绑定两次(每个客户端一次)。 From looking around on msdn and forums it seems to be possible to get around this problem by using setsockopt, but I'm not really sure what changes I should make, and also if this is the smartest solution. 通过在msdn和论坛上四处浏览,似乎可以通过使用setsockopt来解决此问题,但是我不确定如何进行更改,以及这是否是最明智的解决方案。

I mean I want the clients to connect to the same port don't I? 我的意思是我希望客户端连接到同一端口吗? How else will the client program know what to connect to? 客户端程序还如何知道要连接的内容? Or am I just missing something? 还是我只是想念一些东西? As I said I have no prior experience with winsock or any other network programming so I might be doing things in a stupid way. 正如我说的那样,我以前没有Winsock或任何其他网络编程的经验,所以我可能会以愚蠢的方式做事。

int listenOnPort(int portno, SOCKET& reciever, SOCKET s){
int error = WSAStartup(0x0202, &w);

if (error)
{
    cout << "Error starting WSA";
    return false; //for some reason we couldn't start Winsock
}

if (w.wVersion != 0x0202) //Wrong Winsock version?
{
    cout << "Wrong Winsock version";
    WSACleanup();
    return false;
}

SOCKADDR_IN addr; // The address structure for a TCP socket

addr.sin_family = AF_INET; // Address family
addr.sin_port = htons(portno); // Assign port no to this socket

//Accept a connection from any IP using INADDR_ANY
//You could pass inet_addr("0.0.0.0") instead to accomplish the 
//same thing. If you want only to watch for a connection from a 
//specific IP, specify that //instead.
addr.sin_addr.s_addr = htonl(INADDR_ANY);

s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create a socket

if (s == INVALID_SOCKET)
{
    cout << "Couldn't create the socket";
    return false; //Don't continue if we couldn't create a //socket!!
}

if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{
    //We couldn't bind (this will happen if you try to bind to the same  
    //socket more than once)
    cout << "Error binding the socket";
    return false;
}

//Now we can start listening (allowing as many connections as possible to  
//be made at the same time using SOMAXCONN). You could specify any 
//integer value equal to or lesser than SOMAXCONN instead for custom 
//purposes). The function will not //return until a connection request is 
//made
listen(s, 1);

reciever = accept(s, NULL, NULL);
cout << "connection established\n\n";

//Don't forget to clean up with CloseConnection()!}



int main(){
e = WSAGetLastError();
listenOnPort(1337, r1, s1);
cout << "First Client connected\n\n";
e = WSAGetLastError();

listenOnPort(1338, r2, s2);
cout << "Second Client connected\n\n";
e = WSAGetLastError();

std::thread com1(communicate, r1, r2);
std::thread com2(communicate, r2, r1);

com1.join();
com2.join();

//system("pause");

closeConnection();}

You have to have 1 thread in the server dedicated to accept new connections. 服务器中必须有1个专用于接受新连接的线程。

The process listens to new connections and assigns a new port (in the server) for that connection, making the default port available for new connections. 该进程侦听新连接,并为该连接分配一个新端口(服务器中),从而使默认端口可用于新连接。

in the server you will have N+1 socket ports open at any time when N is the number of clients the server has and 1 is the socket listening to new connections. 在服务器中,当N是服务器拥有的客户端数并且1是侦听新连接的套接字时,您将随时打开N + 1个套接字端口。

Take a look at this: http://www.codeproject.com/Articles/7785/Single-Server-With-Multiple-Clients-a-Simple-C-Imp 看看这个: http : //www.codeproject.com/Articles/7785/Single-Server-With-Multiple-Clients-a-Simple-C-Imp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM