Hi, I'm relatively new to socket programming in C/C++ but i have read a few tutorials and had a good go at trying to do it. I have got to the point where i have created a kind server that will accept incoming connections and then when the data is recieved, it will display a message box containing the data it recieved as a string.
Server code:
#include <winsock.h>
#include <windows.h>
void LastError(LPTSTR lpszFunction)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
wsprintf((LPTSTR)lpDisplayBuf,
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Message Server"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
HWND hwnd; //this will later be used for the window handle when the interface is implemented
char * ipaddress; //Temp variable, not yet used...
int main ()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
{
LastError("WSAStartup()");
exit(1);
}
SOCKET listeningSocket;
listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listeningSocket == INVALID_SOCKET)
{
LastError("socket()");
exit(1);
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(1243);
int ret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
if (ret == SOCKET_ERROR)
{
LastError("bind()");
exit(1);
}
ret = listen(listeningSocket,1);
if (ret == SOCKET_ERROR)
{
LastError("listen()");
exit(1);
}
SOCKET client;
client = accept(listeningSocket,NULL,NULL);
if (client == INVALID_SOCKET)
{
LastError("accept()");
send(listeningSocket,"true",4,0);
exit(1);
}
char buffer[256];
ret = recv(listeningSocket,buffer,256,0);
if (ret == SOCKET_ERROR)
{
LastError("recv()");
}
else
{
MessageBox(hwnd,buffer,"Message Server",MB_ICONEXCLAMATION);
}
closesocket(client);
closesocket(listeningSocket);
}
Client code:
#include <winsock.h>
#include <windows.h>
#include <stdio.h>
void LastError(LPTSTR lpszFunction)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
wsprintf((LPTSTR)lpDisplayBuf,
TEXT("Function '%s' failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_ICONERROR);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
HWND hwnd;
char * serverAddress="127.0.0.1";
char buffer[256]="This is the message";
int ret;
int main ()
{
WSADATA wsaData;
ret = WSAStartup(MAKEWORD(1, 1), &wsaData);
if (ret != 0)
{
LastError("WSAStartup()");
exit(1);
}
LPHOSTENT hostEntry;
in_addr iaHost;
iaHost.s_addr = inet_addr(serverAddress);
hostEntry = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
if (!hostEntry)
{
LastError("gethostbyaddr()");
exit(1);
}
SOCKET sendingSocket;
sendingSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sendingSocket == INVALID_SOCKET)
{
LastError("socket()");
exit(1);
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_port = htons(1243);
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
ret = connect(sendingSocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
if (ret == SOCKET_ERROR)
{
LastError("connect()");
exit(1);
}
ret = send(sendingSocket,buffer,256,0);
if (ret == SOCKET_ERROR)
{
LastError("send()");
exit(1);
}
else
{
MessageBox(hwnd,"Message has been sent successfully","Client",MB_ICONINFORMATION);
/* char temp[100]; //not yet finished, ignore this..
sprintf(temp,"%d BYTES sent",ret);
MessageBox(0,temp,0,0); */
}
closesocket(sendingSocket);
}
For now I am using the loopback IP address to I can connect the server and client on the same computer using a random port number "1243".
Currently the server runs fine and the client does also, but when I run the client, and the message is sent, the server does not seem to recieve the message correctly since a message is displayed saying "recv() failed with error 10057: A request to send or recieve data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied." So Im assuming there is some kind of mistake in the server program or I am doing something wrong when sending the message.
Secondly, I want to be able to run the server remotely and send a message to it using the client. My problem is that I am behind a router and the destination computer is behind a router also, in fact the same one. We share the same IP address. I want to be able to know how to connect to the server when it is running on a computer behind a router. So basically I want to know how to specify which computer will recieve the data when there are a number of different computers behind the same router.
Thanks.