如何从其他IP地址连接到服务器?

时间:2019-03-17 09:52:57

标签: c++

我创建了一个服务器和一个客户端。
我在单独的计算机上运行它们,并且它们都连接到单独的wifi网络。
客户端可以从另一个IP地址连接到服务器吗?
如果可能的话,我应该向客户端写入哪个IP地址?

谢谢您的回答。

服务器主。

// Start winsock.
if(WSAStartup(MAKEWORD(2, 2), &WsaDat) != 0)
    error("WinSock initialization failed.");

local.sin_family = AF_INET;
local.sin_addr.s_addr = (INADDR_ANY);
local.sin_port = htons(port);

// Create a SOCKET for connecting to server
if ((ListenSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    error("Socket failed with error.");
else printf("Server: socket() is OK.\n");

// Setup the TCP listening socket
if (bind(ListenSocket, (struct sockaddr*)&local, sizeof(local)) == SOCKET_ERROR)
    error("Bind failed with error.");
else printf("Server: bind() is OK.\n");

// We cannot listen() on a UDP socket.
if (listen(ListenSocket, 5) == SOCKET_ERROR)
    error("Listen failed with error.");

printf("Server: my - %s, port %d\n", inet_ntoa(local.sin_addr), htons(local.sin_port));
printf("I'm listening and waiting connection.\n");

fromLen = sizeof(from);
// Wait the client.
if((msgsock = accept(ListenSocket, (struct sockaddr*)&from, &fromLen)) == INVALID_SOCKET)
    error("Server: accept() error.");
else{
    printf("Server: accept() is OK.\n");
    printf("Server: accepted connection from %s, port %d\n", inet_ntoa(from.sin_addr), htons(from.sin_port));
}
// Listen the client and send the listening data to the client.
do {
    MainLoop();
    Sleep(500);
} while (true);

return 0;

服务器中的MainLoop函数。

// recv.
if((retVal = recv(msgsock, buff, sizeof(buff), 0)) == SOCKET_ERROR)
    error("Server: recv() failed.");
else if(retVal == 0)
    error("Server: Client closed connection.");
else
    printf("Server: recv() is OK.\n");

// send.
retVal = send(msgsock, buff, sizeof(buff), 0);

客户端主体。

// Start winsock.
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2, 2), &WsaDat) != 0)
    return error("WinSock initialization failed.");

// Create socket.
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(Socket == INVALID_SOCKET)
    return error("Socket creation failed.");

// Resolve ip addres for hosname.
struct hostent *host;
if((host = gethostbyname("localhost")) == NULL)
    return error("Failed to resolve hostname");

// Setup our socket address structure.
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(8457);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);

// Connect to server.
if(connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0)
    return error("Failed to establish connection with server.");

// if iMode != 0, non blocking mode is enabled.
u_long iMode = 1;
ioctlsocket(Socket, FIONBIO, &iMode);
return "";

客户端中的recv_data函数,此函数正在运行计时器。

char buff[1000];
int length = recv(Socket, buff, sizeof(buff), 0);
if(length > 0)
    return buff;
else
    return "";

客户端中的send_data函数,该函数正在运行一个按钮。

string sendPacket = name;
sendPacket += " : ";
sendPacket += data;
send(Socket, sendPacket.c_str(), sendPacket.length() + 1, 0);

1 个答案:

答案 0 :(得分:0)

服务器代码:

local.sin_addr.s_addr = (INADDR_ANY);

INNADDR_ANY-表示服务器监听所有IP地址(0.0.0.0)

在客户端代码中,连接前应使用类似的绑定调用。在绑定函数的struct sockaddr参数中,您可以指定客户端ip地址。