windows socket programming in c++ , your first socket(networking) program!

Windows socket programming in c/c++ was frustrating for me when it was first time. But winsock2 is not that hard to make your basic networking program in few minutes. Today i will explain the basic of windows socket programming step by step using winsock2 and c++.  If you don't have understanding in c or c++ then you will not understand this article. So before having basic knowledge in c/c++ you should not start with windows/socket programming. I am fan of gcc compiler so i used gcc(mingw-w64) to compile all the codes(Of this blog).


To write any windows program we are required to include the "windows.h" . And for the socket we need only "winsock2.h". Only two required header we need are:




Well, Let's go step by step with example:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include 
#include


int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - \n"<
}
else{
std::cout<<"Good - Everything fine!\n"<
}

return 0;
}


In line 7 WSAData is a structure name which holds the information about windows socket implementation. So here we declare our own new object to work with called "version".  About WSAData here you will get more in details.

In line 8 MAKEWORD() is a macro which is type of WORD. MAKEWORD(2,2) is going to be "2.2".

In line 9, We store the WSAStartup() function in variable "what" . This function will check if the version is higher or lower. If the version is correct as we expected then it will return value 0 otherwise something else which should be checked by WSAGetLastError() as i did in line 11.

Compile the code and run , if you are in xp+ then you will get output "Good - Everything fine"




Since everything fine , So we want to create our real socket using structure name "SOCKET":


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include 
#include


int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - \n"<
}
else{
std::cout<<"Good - Everything fine!\n"<
}

SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(u_sock==INVALID_SOCKET)
std::cout<<"Creating socket fail\n";

else
std::cout<<"It was okay to create the socket\n";

return 0;
}

Our second step is creating socket. So in line 17 we declare the variable of SOCKET called u_sock and store the socket() function.

AF_INET specify to use ipv4.

SOCK_STREAM to specify that two connection based and reliable which used by AF_INET.

IPPROTO_TCP specify that its Internet Protocol (TCP/IP).

You can get more details in this link http://msdn.microsoft.com/en-us/library/windows/desktop/ms740506%28v=vs.85%29.aspx  .

If something wrong creating the socket then it return the value "INVALID_SOCKET" which we checked in line 18.




Time to specify address and make connection. For bit theory and basic idea read: http://msdn.microsoft.com/en-us/library/windows/desktop/ms740496%28v=vs.85%29.aspx and connect() function http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx.

The code for address information and using connect() function:


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include 
#include


int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - \n"<
}
else{
std::cout<<"Good - Everything fine!\n"<
}

SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(u_sock==INVALID_SOCKET)
std::cout<<"Creating socket fail\n";

else
std::cout<<"It was okay to create the socket\n";

//Socket address information
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("192.168.206.1");
addr.sin_port=htons(80);
/*==========Addressing finished==========*/

//Now we connect
int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
if(conn==SOCKET_ERROR){
std::cout<<"Error - when connecting "<
closesocket(u_sock);
WSACleanup();
}


return 0;
}

In the above example line 25 we declare the object to work with. Then in 26 we specify to go with ipv4 , in line 27 we set our target address to connect to and in line 28 we set port number.

Line number 32 declaring a variable type of int and storing full connect() function for using it later. The parameters u_socks is the socket name we have created in line 17, (SOCKADDR*)&addr mean the address information pointing to SOCKADDR and the length of the all information specified.

Line number 33 to 36 checking if there is anything wrong, connect() function return error code SOCKET_ERROR when unsuccessful. If so we check the Error code number then close the socket using closesocket() function.

Assuming everything Went fine. We are almost done. But without getting some data from Remote host may make you thinking bad. So lets use more two function send() and recv(). Using send() function we send whatever to remote host and using recv we store the output to array:


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include 
#include
#include



int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - \n"<
}
else{
std::cout<<"Good - Everything fine!\n"<
}

SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(u_sock==INVALID_SOCKET)
std::cout<<"Creating socket fail\n";

else
std::cout<<"It was okay to create the socket\n";

//Socket address information
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("192.168.206.1");
addr.sin_port=htons(80);
/*==========Addressing finished==========*/

//Now we connect
int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
if(conn==SOCKET_ERROR){
std::cout<<"Error - when connecting "<
closesocket(u_sock);
WSACleanup();
}

//Send some message to remote host
char* mymsg="GET / HTTP/1.1\r\n\r\n";
char vect[512]={0};

int smsg=send(u_sock,mymsg,sizeof(mymsg),0);
if(smsg==SOCKET_ERROR){
std::cout<<"Error: "<
WSACleanup();
}

int get=recv(u_sock,vect,512,0);
if(get==SOCKET_ERROR){
std::cout<<"Error in Receiving: "<
}
std::cout<
clossocket(u_sock);
return 0;
}

In line 42 we define a string to send to remote address. Line 43 declaring an array to hold output of the command.

Line number 45 , we store the send() and the required parameters . u_sock is the name of the created socket , mymsg has the command to send to the remote address, Maximum length of the command, And the flag.

In the line 51 , we used recv() to get output and store to an array.  The parameters of this function same as send().

 Line number 55, Getting the output from the array and printing in the screen then line 56 closing the socket.




I hope i made this article simpler to understand. I don't really like to write too much theory since there are lots and not that useful to me(may be to you too).  If you have any questions , please comment!


Hope you enjoyed!