Skip to content

Instantly share code, notes, and snippets.

@JubbaSmail
Created January 4, 2015 12:43
Show Gist options
  • Save JubbaSmail/5b70e1ed1e8ecebd127b to your computer and use it in GitHub Desktop.
Save JubbaSmail/5b70e1ed1e8ecebd127b to your computer and use it in GitHub Desktop.
// WIN SOCK - TCP Client example
#include <winsock2.h>
#include <stdio.h>
#if define UNICDOE
#define wsprintf wsprintfW
#else
#define wsprintf wsprintfA
#endif
SOCKET init_tcp()
{
WSADATA wsaData;
//Check if Operating System support WS2_32.LIB
int retval = WSAStartup(0x202, // version number 2.2
&wsaData // struct contain DLL configuration information
);
if (retval != 0)
return INVALID_SOCKET;
printf("Server: WSAStartup() is OK.\n");
//=======================================================
// TCP socket
SOCKET channel = socket(AF_INET, // AF_INET = TCP/IP | PF_INET
SOCK_STREAM,// SOCK_STREAM = TCP | SOCK_DGRAM = UDP
0 // 0 when use AF_INET
);
if (channel == INVALID_SOCKET)
return INVALID_SOCKET;
return channel;
}
SOCKET open_tcp(SOCKET channel, char *server_ip, unsigned short server_port)
{
unsigned int addr = inet_addr(server_ip);
struct hostent *hp;
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
if (hp == NULL)
return INVALID_SOCKET;
printf("Client: gethostbyaddr() is OK.\n");
// Copy the resolved information into the sockaddr_in structure
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(server_port);
printf("Client: Client connecting to: %s.\n", hp->h_name);
if (connect(channel, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
return INVALID_SOCKET;
printf("Client: connect() is OK.\n");
return channel;
}
int recieve_tcp(SOCKET channel, char Buffer[],int length)
{
int retval = recv(channel, Buffer, length, 0);
if (retval == SOCKET_ERROR)
return SOCKET_ERROR;
return retval;// number of bytes recieved
}
int send_tcp(SOCKET channel, char Buffer[],int length)
{
int retval = send(channel, Buffer, length, 0);
if (retval == SOCKET_ERROR)
return SOCKET_ERROR;
return retval;// number of bytes recieved
}
int main(int argc, char **argv)
{
SOCKET channel = init_tcp();
if (channel == INVALID_SOCKET)
{
printf("Client: init socket failed: error %d\n", WSAGetLastError());
return -1;
}
//===============================
char *server_ip = "127.0.0.1";
unsigned short server_port = 5656;
open_tcp(channel, server_ip, server_port);
if (channel == INVALID_SOCKET)
{
printf("Client: open socket failed: error %d\n", WSAGetLastError());
return -1;
}
//===============================
// Test sending some string
printf("Please enter your message:\n");
char line[1024]; /* Generously large value for most situations */
line[0] = '\0'; /* Ensure empty line if no input delivered */
line[sizeof(line)-1] = ~'\0'; /* Ensure no false-null at end of buffer */
fgets(line, sizeof(line), stdin);
//===============================
char Buffer[128];
wsprintf(Buffer, line);
int retval = send_tcp(channel, Buffer,sizeof(Buffer));
if (retval == SOCKET_ERROR)
printf("Client: send() failed: error %d\n", WSAGetLastError());
memset(&Buffer, 0, sizeof(Buffer));
retval = recieve_tcp(channel, Buffer, sizeof(Buffer));
if (retval == SOCKET_ERROR)
{
printf("Client: recieved from socket failed: error %d\n", WSAGetLastError());
closesocket(channel);
}
else if (retval == 0)
{
printf("Client: Server closed connection.\n");
closesocket(channel);
WSACleanup();
return -1;
}
else
printf("Client: Received: %d bytes, data from server: %s \n", retval, Buffer);
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment