S
Syed Babu
Guest
Im new to socket programming. I have created a socket server and the socket client.
Here is my socket client code:
int _tmain(int argc, _TCHAR* argv[])
{
int iResult = -1;
char szPath[128] = "";
WSADATA wsaData;
//Intialize the win socket dll by passing highest version of windows socket specification
WSAStartup(MAKEWORD(2, 2), &wsaData);
//Get the host name
gethostname(szPath, sizeof(szPath));
//Get the host by name and fetch the id address
hostent* pHostEnt = gethostbyname(szPath);
in_addr* pInAddress = (in_addr*)(*pHostEnt->h_addr_list);
char* szIPAddresss = inet_ntoa(*pInAddress);
//Create an unnamed socket for the server
int client_socket_fd = socket(AF_INET,SOCK_STREAM,0 );
//Connect the client to the port
sockaddr_in client_address;
client_address.sin_family = AF_INET;
client_address.sin_addr = *pInAddress;
client_address.sin_port = 50000;
int client_addr_len = sizeof(sockaddr_in);
iResult = connect(client_socket_fd, (struct sockaddr *)&client_address, client_addr_len);
if( iResult != 0 )
{
int iErrCode = WSAGetLastError();
cout<<iErrCode<<endl;
}
char ch = X;
DWORD nbBytesWritten = 0;
WriteFile((HANDLE)client_socket_fd,&ch,1,&nbBytesWritten,NULL );
WSACleanup();
return 0;
}
WriteFile call is getting failed and it returns FALSE.
The connect call is getting succeeded and returns 0.
Not sure what is wrong.I could not use write function as VS does not support it. What is the problem with WriteFile call?
Continue reading...
Here is my socket client code:
int _tmain(int argc, _TCHAR* argv[])
{
int iResult = -1;
char szPath[128] = "";
WSADATA wsaData;
//Intialize the win socket dll by passing highest version of windows socket specification
WSAStartup(MAKEWORD(2, 2), &wsaData);
//Get the host name
gethostname(szPath, sizeof(szPath));
//Get the host by name and fetch the id address
hostent* pHostEnt = gethostbyname(szPath);
in_addr* pInAddress = (in_addr*)(*pHostEnt->h_addr_list);
char* szIPAddresss = inet_ntoa(*pInAddress);
//Create an unnamed socket for the server
int client_socket_fd = socket(AF_INET,SOCK_STREAM,0 );
//Connect the client to the port
sockaddr_in client_address;
client_address.sin_family = AF_INET;
client_address.sin_addr = *pInAddress;
client_address.sin_port = 50000;
int client_addr_len = sizeof(sockaddr_in);
iResult = connect(client_socket_fd, (struct sockaddr *)&client_address, client_addr_len);
if( iResult != 0 )
{
int iErrCode = WSAGetLastError();
cout<<iErrCode<<endl;
}
char ch = X;
DWORD nbBytesWritten = 0;
WriteFile((HANDLE)client_socket_fd,&ch,1,&nbBytesWritten,NULL );
WSACleanup();
return 0;
}
WriteFile call is getting failed and it returns FALSE.
The connect call is getting succeeded and returns 0.
Not sure what is wrong.I could not use write function as VS does not support it. What is the problem with WriteFile call?
Continue reading...