-
March 19th, 2013, 11:15 AM
#1
question about broadcast code
i created a server application in mfc and the client is created using a console application.
the server displays the ip address of any connect client in a list box.
in the client i have the line ServerAddr.sin_addr.s_addr = inet_addr("10.11.13.112"); which means the client can only connect to a server on one machine.
but by using broadcasting (INADDR_BROADCAST) i can put the server on any machine
what do i need to change in my code to do this?
server:
Code:
unsigned __stdcall tcp_server(void *param)
{
int port = 7171;
if (param)
port = reinterpret_cast<short>(param);
SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == -1)
{
closesocket(s);
return 1;
}
sockaddr_in addr_in;
addr_in.sin_family = AF_INET;
addr_in.sin_addr.s_addr = INADDR_ANY;
addr_in.sin_port = htons(port);
int bind_ret = bind(s, (sockaddr*)&addr_in, sizeof(addr_in));
if (bind_ret == -1)
{
closesocket(s);
return 1;
}
int listen_ret = listen(s, 5);
if (listen_ret == -1)
{
closesocket(s);
return 1;
}
while (true)
{
sockaddr_in client_addr;
int len = sizeof(client_addr);
SOCKET client_sock = accept(s, (sockaddr*)&client_addr, &len);
ClientInfo info;
info.sock = client_sock;
info.addr = inet_ntoa(client_addr.sin_addr);
{
Mutex<CriticalSection>::Lock lock(client_cs);
clients.push_back(info);
}
unsigned tid;
_beginthreadex(NULL, 0, tcp_servers_client, reinterpret_cast<void*>(client_sock), 0, &tid);
}
closesocket(s);
return 0;
}
client:
Code:
int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET SendingSocket;
SOCKADDR_IN ServerAddr, ThisSenderInfo;
// Server/receiver port to connect to
unsigned int Port = 7171;
int RetCode;
int nlen;
// Initialize Winsock version 2.2
WSAStartup(MAKEWORD(2,2), &wsaData);
SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(SendingSocket == INVALID_SOCKET)
{
printf("Client: socket failed! Error code: %ld\n", WSAGetLastError());
WSACleanup();
return -1;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
RetCode = connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
if(RetCode != 0)
{
printf("Client: connect failed! Error code: %ld\n\n", WSAGetLastError());
closesocket(SendingSocket);
// Do the clean up
WSACleanup();
return -1;
}
else
{
printf("Client: got connected...\n");
}
getsockname(SendingSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
printf("Client: Server IP(s) used: %s\n", inet_ntoa(ServerAddr.sin_addr));
printf("Client: Server port used: %d\n\n", htons(ServerAddr.sin_port));
memset(&ThisSenderInfo, 0, sizeof(ThisSenderInfo));
nlen = sizeof(ThisSenderInfo);
getsockname(SendingSocket, (SOCKADDR *)&ThisSenderInfo, &nlen);
printf("Client: Client IP(s) used: %s\n", inet_ntoa(ThisSenderInfo.sin_addr));
printf("Client: Client port used: %d\n", htons(ThisSenderInfo.sin_port));
}
if( shutdown(SendingSocket, SD_SEND) != 0)
{
printf("Client: There is something wrong with the shutdown. The error code: %ld\n", WSAGetLastError());
}
else
{
printf("\nClient: shutdown working...\n");
}
if(closesocket(SendingSocket) != 0)
{
printf("Client: Cannot close \"SendingSocket\" socket. Error code: %ld\n", WSAGetLastError());
}
else
{
printf("Client: Closing \"SendingSocket\" socket...\n");
}
// When your application is finished handling the connection, call WSACleanup.
if(WSACleanup() != 0)
{
printf("Client: WSACleanup failed!...\n");
}
else
{
printf("Client: WSACleanup worked...\n");
}
system ("PAUSE");
return 0;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|