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

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

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:

<iostream>
<winsock2.h>

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 <iostream>
#include <winsock2.h>


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"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!n"<<std::endl;
}

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 <iostream>
#include <winsock2.h>


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"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!n"<<std::endl;
}

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

else
std::cout<<"It was okay to create the socketn";

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 <iostream>
#include <winsock2.h>


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"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!n"<<std::endl;
}

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

else
std::cout<<"It was okay to create the socketn";

//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 "<<WSAGetLastError()<<std::endl;
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 <iostream>
#include <winsock2.h>
#include <string>



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"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!n"<<std::endl;
}

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

else
std::cout<<"It was okay to create the socketn";

//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 "<<WSAGetLastError()<<std::endl;
closesocket(u_sock);
WSACleanup();
}

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

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

int get=recv(u_sock,vect,512,0);
if(get==SOCKET_ERROR){
std::cout<<"Error in Receiving: "<<WSAGetLastError()<<std::endl;
}
std::cout<<vect<<std::endl;
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!

 

debian apt-get or aptitude update Hash Sum mismatch

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

We need “apt-get update” to update the debian source list if we want to upgrade debian to next release. When i was going to upgrade debian , i got error “Hash Sum mismatch” something like:

W: Failed to fetch bzip2:/var/lib/apt/lists/partial/mirrors.yourmirror.com_debian_dists_wheezy_main_i18n_Translation-en  Hash Sum mismatch

E: Some index files failed to download. They have been ignored, or old ones used instead.

It happened for several times in the past, Today, when i was going to upgrade debian 7 to 7.1.0 i got this error again.

Anyway, It happened for the package server. So i decide to change the server address from the sources.list . To select fast server i used “netselect-apt” :

apt-get install netselect-apt && cd /etc/apt/ && netselect-apt -n wheezy -o sources.list

This command will install the netselect-apt and find the fast server then replace the old sources.list. It will output like:

 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
Duplicate address 64.50.233.100 (http://64.50.233.100/debian/, http://ftp-nyc.osuosl.org/debian/); keeping only under first name.
netselect: 43 (23 active) nameserver request(s)...
Duplicate address 128.30.2.36 (http://128.30.2.36/debian/, http://debian.lcs.mit.edu/debian/); keeping only under first name.
netselect: unknown host debian.comu.edu.tr
netselect: 17 (17 active) nameserver request(s)...
Duplicate address 128.61.240.89 (http://128.61.240.89/debian/, http://debian.gtisc.gatech.edu/debian/); keeping only under first name.
Running netselect to choose 10 out of 398 addresses.
...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
The fastest 10 servers seem to be:

http://mirror.positive-internet.com/debian/
http://mirror.0x.sg/debian/

http://mirror.sov.uk.goscomb.net/debian/
http://mirror.vorboss.net/debian/
http://archive.mmu.edu.my/debian/
http://debian.mirrors.ovh.net/debian/
http://ukdebian.mirror.anlx.net/debian/
http://ukdebian.mirror.anlx.net/debian/
http://opensource.nchc.org.tw/debian/

Of the hosts tested we choose the fastest valid for HTTP:
http://mirror.0x.sg/debian/

Writing netselect-apt.list.
Done.

You can exclude your local server and replace with working fast server if netselect-apt selecting the same server for you.

Hope this post will help someone!