Click here to Skip to main content
Click here to Skip to main content

Beginning Winsock Programming - Simple TCP server

By , 25 Feb 2002
 

Introduction

The WinSock (Windows Sockets) API is a socket programming library for Microsoft Windows Operating Systems. It was originally based on Berkeley sockets. But several Microsoft specific changes were employed. In this article I shall attempt to introduce you to socket programming using WinSock, assuming that you have never done any kind of network programming on any Operating System.

If you only have a single machine, then don't worry. You can still program WinSock. You can use the local loop-back address called localhost with the IP address 127.0.0.1. Thus if you have a TCP server running on your machine, a client program running on the same machine can connect to the server using this loop-back address.

Simple TCP Server

In this article I introduce you to WinSock through a simple TCP server, which we shall create step by step. But before we begin, there are a few things that you must do, so that we are truly ready for starting our WinSock program

  • Initially use the VC++ 6.0 App Wizard to create a Win32 console application. 
  • Remember to set the option to add support for MFC
  • Open the file stdafx.h and add the following line :- #include <winsock2.h>
  • Also #include conio.h and iostream just after winsock2.h
  • Take Project-Settings-Link and add ws2_32.lib to the library modules list.

The main function

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;	
	
    cout << "Press ESCAPE to terminate program\r\n";
    AfxBeginThread(ServerThread,0);
    while(_getch()!=27);
	
    return nRetCode;
}

What we do in our main() is to start a thread and then loop a call to _getch(). _getch() simply waits till a key is pressed and returns the ASCII value of the character read. We loop till a value of 27 is returned, since 27 is the ASCII code for the ESCAPE key. You might be wondering that even if we press ESCAPE, the thread we started would still be active. Don't worry about that at all. When main() returns the process will terminate and the threads started by our main thread will also be abruptly terminated.

The ServerThread function

What I will do now is to list our ServerThread function and use code comments to explain what each relevant line of code does. Basically what our TCP server does is this. It listens on port 20248, which also happens to be my Code Project membership ID. Talk about coincidences. When a client connects, the server will send back a message to the client giving it's IP address and then close the connection and go back to accepting connections on port 20248. It will also print a line on the console where it's running that there was a connection from this particular IP address. All in all, an absolutely useless program, you might be thinking. In fact some of you might even think this is as useless as SNDREC32.EXE which comes with Windows. Cruel of those people I say.

UINT  ServerThread(LPVOID pParam)
{	
    cout << "Starting up TCP server\r\n";

    //A SOCKET is simply a typedef for an unsigned int.
    //In Unix, socket handles were just about same as file 
    //handles which were again unsigned ints.
    //Since this cannot be entirely true under Windows
    //a new data type called SOCKET was defined.
    SOCKET server;

    //WSADATA is a struct that is filled up by the call 
    //to WSAStartup
    WSADATA wsaData;

    //The sockaddr_in specifies the address of the socket
    //for TCP/IP sockets. Other protocols use similar structures.
    sockaddr_in local;

    //WSAStartup initializes the program for calling WinSock.
    //The first parameter specifies the highest version of the 
    //WinSock specification, the program is allowed to use.
    int wsaret=WSAStartup(0x101,&wsaData);

    //WSAStartup returns zero on success.
    //If it fails we exit.
    if(wsaret!=0)
    {
        return 0;
    }

    //Now we populate the sockaddr_in structure
    local.sin_family=AF_INET; //Address family
    local.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
    local.sin_port=htons((u_short)20248); //port to use

    //the socket function creates our SOCKET
    server=socket(AF_INET,SOCK_STREAM,0);

    //If the socket() function fails we exit
    if(server==INVALID_SOCKET)
    {
        return 0;
    }

    //bind links the socket we just created with the sockaddr_in 
    //structure. Basically it connects the socket with 
    //the local address and a specified port.
    //If it returns non-zero quit, as this indicates error
    if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
    {
        return 0;
    }

    //listen instructs the socket to listen for incoming 
    //connections from clients. The second arg is the backlog
    if(listen(server,10)!=0)
    {
        return 0;
    }

    //we will need variables to hold the client socket.
    //thus we declare them here.
    SOCKET client;
    sockaddr_in from;
    int fromlen=sizeof(from);

    while(true)//we are looping endlessly
    {
        char temp[512];

        //accept() will accept an incoming
        //client connection
        client=accept(server,
            (struct sockaddr*)&from,&fromlen);
		
        sprintf(temp,"Your IP is %s\r\n",inet_ntoa(from.sin_addr));

        //we simply send this string to the client
        send(client,temp,strlen(temp),0);
        cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";
		
        //close the client socket
        closesocket(client);

    }

    //closesocket() closes the socket and releases the socket descriptor
    closesocket(server);

    //originally this function probably had some use
    //currently this is just for backward compatibility
    //but it is safer to call it as I still believe some
    //implementations use this to terminate use of WS2_32.DLL 
    WSACleanup();

    return 0;
}

Testing it out

Run the server and use telnet to connect to port 20248 of the machine where the server is running. If you have it on the same machine connect to localhost.

Sample Output

We see this output on the server

E:\work\Server\Debug>server
Press ESCAPE to terminate program
Starting up TCP server
Connection from 203.200.100.122
Connection from 127.0.0.1
E:\work\Server\Debug>

And this is what the client gets

nish@sumida:~$ telnet 202.89.211.88 20248
Trying 202.89.211.88...
Connected to 202.89.211.88.
Escape character is '^]'.
Your IP is 203.200.100.122
Connection closed by foreign host.
nish@sumida:~$

Conclusion

Well, in this article you learned how to create a simple TCP server. In further articles I'll show you more stuff you can do with WinSock including creating a proper TCP client among other things. If anyone has problems with compiling the code, mail me and I shall send you a zipped project. Thank you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nish Sivakumar
United States United States
Member
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralZipped Project FilememberDiplomat25 Jul '12 - 22:53 
Please send me the zipped project file. I have problems compiling your code correctly.

Thank you and best wishes for you and your family.
 
Victor
vnla@hotmail.com
GeneralMy vote of 5memberHumberto Raposa18 Jun '12 - 9:10 
This is really nice piece of work. I most enjoyed the simple explanations and direct example.
Questionwaiting for project filememberbyronli7 Jun '12 - 15:42 
Hi Nishant Sivakumar,
 
Can you send me the project file to my email address at byronli66@qq.com?
 
Thanks.
 
best Regards
Questionsend file to server through http requestmemberritesh 20106 Mar '12 - 20:56 
hi. nish
i have to write a program in which the client send the http request to server for receive the file .server provide the client a http url and on hitting the url the client data send to server.
i am using window environment + visual st. for writing program in c lang.
i have make client-server program using winsock api. which work fine for sending data but in this case the send has to listen for client and then client execute.
But i want that client send req. to server url and file transfer.how it can be if we send file from client side how server received it from it end.Confused | :confused: help
QuestionPlease send me the zipped project?memberfabmkk12 Feb '12 - 20:03 
Would it be possible for you to send me the zipped project file? I have problem compiling your code correctly and maybe i could learn more by having the actual project file.
 
Thanks you and wishes you and your family the best.
 
fabmkk@gmail.com
QuestionProject Filememberikurtoglu19 Jan '12 - 1:45 
Hi Nishant Sivakumar,

Can you send me the project file to my email address at ikurtoglu@yahoo.com?

Thanks.

Regards,
Ismail
Generalproject filemembersheran aus17 Jun '11 - 20:29 
sorry can u send to sheran_silva@hotmail.com
 
that would be much appreciated
 
sheran
Generalproject filemembersheran aus17 Jun '11 - 20:28 
hey can u send me the file
 
thx
GeneralCan you send me the project file?membercafukarfoo25 May '11 - 22:22 
Hi Nishant Sivakumar,
 
Can you send me the project file to my email address at cafukarfoo@yahoo.com?
 
Thanks.
 
Regards,
Chong kar foo
GeneralMy vote of 5memberzhllq@126.com25 Mar '11 - 6:49 
Dear Nis!
Excellent Article!
Thanks!
Generalneed zip file!!!memberaarthi krishnan15 Jul '10 - 2:30 
dear Mr.Nishant,
i need the source code for this project.i am not able to compile it.can you please send me the zip file to my email id, aarthi.krishnan@yahoo.com
 
thank you
aarthi krishnan
Generalthanks and imemberamir farkhondeh27 Jun '10 - 22:09 
thanks for this code and can you send zip file of it.
Generalcode.zipmemberneu_lake15 Mar '10 - 17:26 
hello,i was wondering if you could help me in a beginners and i would like to learn socket programming was wondering if you could send me the zip file for this code thank you my email is
 

"hwzlake@163.com"
 

 

thanks
QuestionError during compiling..... i m using windows Vistamemberpratikmota4 Feb '10 - 10:15 
Compiling...
server.cpp
h:\vc++ exercies in vs 6.0\server\server.cpp(4) : fatal error C1083: Cannot open precompiled header file: 'Debug/server.pch': No such file or directory
Error executing cl.exe.
 
server.obj - 1 error(s), 0 warning(s)
 

 
How to solve....
 
i have choose radio button as MFC suport, Add this 2 code to server.cpp file.
also add ws2_32.lib to setting-->link
 
server.cpp
---------------------------------------------------------------------------------------
 
// server.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include "server.h"
 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
 
CWinApp theApp;
#include&lt;windows.h&gt;
#include &lt;afxwin.h&gt;
 
using namespace std;
 
iint _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
      int nRetCode = 0;    
    
      cout &lt;&lt; "Press ESCAPE to terminate program\r\n";
      AfxBeginThread(ServerThread,0);
      while(_getch()!=27);
    
      return nRetCode;
}
 

UINT   ServerThread(LPVOID pParam)
{    
      cout &lt;&lt; "Starting up TCP server\r\n";
 
      //A SOCKET is simply a typedef for an unsigned int.
 
      //In Unix, socket handles were just about same as file
 
      //handles which were again unsigned ints.
 
      //Since this cannot be entirely true under Windows
 
      //a new data type called SOCKET was defined.
 
      SOCKET server;
 
      //WSADATA is a struct that is filled up by the call
 
      //to WSAStartup
 
      WSADATA wsaData;
 
      //The sockaddr_in specifies the address of the socket
 
      //for TCP/IP sockets. Other protocols use similar structures.
 
      sockaddr_in local;
 
      //WSAStartup initializes the program for calling WinSock.
 
      //The first parameter specifies the highest version of the
 
      //WinSock specification, the program is allowed to use.
 
      int wsaret=WSAStartup(0x101,&amp;wsaData);
 
      //WSAStartup returns zero on success.
 
      //If it fails we exit.
 
      if(wsaret!=0)
      {
            return 0;
      }
 
      //Now we populate the sockaddr_in structure
 
      local.sin_family=AF_INET; //Address family
 
      local.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
 
      local.sin_port=htons((u_short)20248); //port to use
 

      //the socket function creates our SOCKET
 
      server=socket(AF_INET,SOCK_STREAM,0);
 
      //If the socket() function fails we exit
 
      if(server==INVALID_SOCKET)
      {
            return 0;
      }
 
      //bind links the socket we just created with the sockaddr_in
 
      //structure. Basically it connects the socket with
 
      //the local address and a specified port.
 
      //If it returns non-zero quit, as this indicates error
 
      if(bind(server,(sockaddr*)&amp;local,sizeof(local))!=0)
      {
            return 0;
      }
 
      //listen instructs the socket to listen for incoming
 
      //connections from clients. The second arg is the backlog
 
      if(listen(server,10)!=0)
      {
            return 0;
      }
 
      //we will need variables to hold the client socket.
 
      //thus we declare them here.
 
      SOCKET client;
      sockaddr_in from;
      int fromlen=sizeof(from);
 
      while(true)//we are looping endlessly
 
      {
            char temp[512];
 
            //accept() will accept an incoming
 
            //client connection
 
            client=accept(server,
                  (struct sockaddr*)&amp;from,&amp;fromlen);
         
            sprintf(temp,"Your IP is %s\r\n",inet_ntoa(from.sin_addr));
 
            //we simply send this string to the client
 
            send(client,temp,strlen(temp),0);
            cout &lt;&lt; "Connection from " &lt;&lt; inet_ntoa(from.sin_addr) &lt;&lt;"\r\n";
         
            //close the client socket
 
            closesocket(client);
 
      }
 
      //closesocket() closes the socket and releases the socket descriptor
 
      closesocket(server);
 
      //originally this function probably had some use
 
      //currently this is just for backward compatibility
 
      //but it is safer to call it as I still believe some
 
      //implementations use this to terminate use of WS2_32.DLL
 
      WSACleanup();
 
      return 0;
}
QuestionNot able to sniff TCP packet on windows Vista. But could do so on XPmemberAseem Sharma21 Dec '09 - 21:50 
Hi Nishant,
 
I am having just one month of experience in network programming. I want to trace TCP packets using winsock. I could right the code for that as below. It worked on XP 32 bit but not on Vista Home 32 bit machine.
 
SOCKET	sniffSocket;
if((sniffSocket= socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)
{	
  return 0;
}
struct sockaddr_in src;
memset(&src, 0, sizeof(src));
src.sin_addr.S_un.S_addr = inet_addr (pAdapterInfo->IpAddressList.IpAddress.String);
// pAdapterInfo is the adapter as selected by the user
src.sin_family = AF_INET;
src.sin_port = 0;
 
if (bind(sniffSocket,(struct sockaddr *)&src, sizeof(src)) == SOCKET_ERROR)
{	
  return 0;
}
 
int j=1;
if (WSAIoctl(sniffSocket, SIO_RCVALL, &j, sizeof(j), 0, 0, &in, 0, 0) == SOCKET_ERROR)
{	
  return 0;
}
 
char *pkt_data = (char *)malloc(65536); //Its Big!
int res;
do
{
  res = recvfrom(sniffSocket, pkt_data, 65536, 0, 0, 0); //Eat as much as u can
  ...
  ...
}
while (res > 0);
...
...
 
The above code is working fine on XP. I could trace TCP/IP packets (in variable pkt_data) this way. But when I run the same code on Vista, I am able to trace just UDP/IP packets. Can you tell me why is it not working on Vista?
 
I hope you can resolve my problem.
 
My second query: When we type a web-site address on a browser, we don't tell which adapter to connect to. Browser automatically opens adapter that is connected to internet. How it does that? I mean how can I detect which adapter is being used for internet connection?
 
Thanks in Advance
Regards
Aseem
 
modified on Wednesday, December 23, 2009 1:48 AM

GeneralWould you please send me a copy of the source code? Thank you a lot!memberbeyondfe29 Oct '09 - 17:06 
As title
GeneralI will need the zip file too for this project.memberYogesh Ithape27 Oct '09 - 2:28 
Hi Nishant !!
 
Thnx for the excellent Post , however can you send the project zip file to my email id
 
yithape@gmail.com
GeneralPlease mail me a project ZIP filememberLintao Wang21 Oct '09 - 11:45 
Dear Mr.Nishant,
 
can you plz send me the zip file for this project? My email is lwang@u-systems.com. Thank you!
Smile | :)
Generalzip filememberboon8717 Sep '09 - 3:34 
Dear Mr.Nishant,
 
can you plz send me the zip file for this project? i have tried few times but not succeed to compile it. my email is jd_boon@hotmail.com. Thx
Generalcodememberkufeji3 Aug '09 - 9:03 
hello was wondering if you could help me im a beginners and i would like to learn socket programming was wonmdering if you could send me the zip file for this code thank you my email is
 

"siu07tk@rdg.ac.uk"
 

 

thanks
Questioncould you give me help?memberzhaozhihong28 Jun '09 - 21:09 
Dear Mr.Nishat:
I am a new learner,i have several probems that i cannot sovle itmyself,can you send me the zip file please?my e-mail:zhaozhihong1008@gmail.com
Best wishes
zhaozhihong
General[Message Deleted]memberit.ragester2 Apr '09 - 21:54 
[Message Deleted]
Questioncan you send me the zip file please?memberlovelr8 Oct '08 - 16:36 
Dear Mr.Nishat:
I am a new learner,i have several probems that i cannot sovle itmyself,can you send me the zip file please?my e-mail:lovelr1209@163.com
Best wishes
lovelr
QuestionI can't see the file ?memberkuojui15 Aug '08 - 2:53 
Dear Mr.Nishat
 
Can you please send me the zip file for this project...My e-mail is: justin73125@yahoo.com.tw
Thank you.
 
Best Regards,
Justin
GeneralVS2005memberAhmed Hozayen4 May '08 - 3:22 
hey everyone, I am a rookie in network programming. Does this code compile succesfully on VS2005????
I am having troubles with windows.h
GeneralThe source codememberAhmed Hozayen24 Apr '08 - 5:34 
Dear Mr.Nishat
 
Can you please send me the zip file for this project...My e-mail is: 7ozayen@gmail.com
Thank you.
 
Best Regards,
Ahmed Hozayen,
QuestionRe: The source codememberflik16 Oct '08 - 8:57 
I need it too Smile | :)
pacckal@mail.ru thx!
Questionediting standard include file?memberbkelly1328 Dec '07 - 12:09 
From the article:
 
Open the file stdafx.h and add the following line :- #include <winsock2.h>
Also #include conio.h and iostream just after winsock2.h
 
Why should I, a novice at TCP/IP programming, be required to edit any of the standard include files? Is there no way to make the code work without editing these standard delivered files?
 
Thanks for your time

AnswerRe: editing standard include file?mvpNishant Sivakumar28 Dec '07 - 13:31 
stdafx.h is not a standard C++ header file. It's just the default header file Visual Studio generates for you in which you can put all the common header files. The difference is that this header file is pre-compiled by VC++ (unless the compiler setting is set not to use precompiled headers). While I don't remember the exact compiler switch, you can get it to use a different name (than stdafx) if you want to, but this is the default name.
 

Questionlimiti access to localhostmembersimonecristiano21 Oct '07 - 11:00 
How i can limit the access at the server only from localhost?
 
tnx
GeneralExcellent !memberquanghao30 Sep '07 - 21:56 
Thanks for your article.
GeneralNo MFC [modified]memberSam Hobbs26 Jul '07 - 21:37 
The only need for MFC is AfxBeginThread, and _beginthreadex can be used instead of it, as in:
 
hThread = (HANDLE)_beginthreadex(NULL, 0, ServerThread, 0, 0, &ThreadId);
if (hThread == 0) {
ErrorNumber = errno;
DOSErrorNumber = _doserrno;
return 0;
}

 
Then the few variables used there are:
 
unsigned ThreadId;
HANDLE hThread;
int ErrorNumber, DOSErrorNumber;

 
And ServerThread could be:
 
unsigned __stdcall ServerThread(void *pArgument)
 
Then instead of #include afxwin.h we need to #include windows.h and #include process.h.
 
I might have overlooked one or two details, but my modifications worked for me using _beginthreadex instead of AfxBeginThread.
 

 

 
-- modified at 3:45 Friday 27th July, 2007
GeneralRe: No MFCmemberjc kamaraj13 May '08 - 6:39 
i even changed it to use boost thread.
as such killing the thread is kind of a hassle.
this will start the thread
 
define
the variable
boost::thread *m_pThread;
 
and start the thread as
 
m_pThread = new boost::thread(boost::bind(&jcTCP::ServerThread, this));
 
this worked for me. i like the boost thread better than the windows thread because of the portability issue
QuestionProbblem while Connecting with localhostmemberreena vade25 Jul '07 - 9:11 
Excellent code nishant,but i only get the msg"starting up TCP server.I do not get the server and client IP address.Also I have 2 computers and I wish to connect the other as client.Can I connect these two computers using telnet?if yes,how?Kindly help me out in the step by step procedure of connection after running the code.
Urgent!!!!!!Confused | :confused:
 
Best Regards!

AnswerRe: Probblem while Connecting with localhostmemberSam Hobbs26 Jul '07 - 21:01 
I am not the author of this article, but I am nearly certain that it is not possible to connect two computers using this code. This is only the server portion. The client is telnet. This is a very simple sample. Have you looked at the subsequent articles?
 
To rest this program, use telnet in the manner I showed in answers below.
GeneralRe: Problem while Connecting with localhostmemberreena vade30 Jul '07 - 23:51 
Thank you Sam.But I disagree with you,this code alone along with the subsequent code for the client can be used to connect two computers,albeit you need to introduce some changes.Infact I would suggest all the coders to introduce some simple msgs like send(on the client side),recv(provided the client is sending sumthing,eg.a simple text msg,a file,etc)on the server side in this code.Same needs to be done on the client side as well.This will enable the server and client to communicate very easily.
Smile | :)
 
Best Regards!

GeneralRe: Problem while Connecting with localhostmemberSam Hobbs13 Aug '07 - 18:37 
reena vade wrote:
albeit you need to introduce some changes

 
Exactly. That is the essential point. If the code is used as-is then it does not connect two computers. That is all I meant to say. It is a start but it needs more, even if it is just a little bit more that it needs.
GeneralError when I use number port 80memberAntonio292923 May '07 - 4:49 

 

Because if I use number port 80 ( htons(80) ) I have an error on bind instruction?
If I use another number port I have not error on bind instruction?
 
Thanks

GeneralRe: Error when I use number port 80memberzhaozhihong28 Jun '09 - 21:15 
Because you have used the port 80 .the web site's defaulted port is 80!
Questionsgfs --- secured global file systemmemberarunwasgood5 Apr '07 - 2:38 
hi,
 
after seeing the whole site i felt your athe only jame who can help me
do you know some thing about this
we have to create some thing like this
 
we have to split a file to be secured and it is split into required no of parts and each part is encypted and sent to various systems in the network so we use socket program to
send files and problem here is we have different clients and
so we have tos send perticular files to each client here you have given the host address in sockSrvr how do we get the address we want how do i do it.

Another thing is when ever a person from the client side wants to open the complete file all the files from the network which were sned to each client from srever,have to gathered and decrypted and joined and open the full file when we give a command from client how do i do this this.

is this posible i can give you a sample code but i feel its not correct can you just give me a code of this case
 
like when the client wants to open the full file
he has to passon the some protocal to server and server gets the information of all the file from the notepad we manintain and then call all the clients to send the files and then server joins all the files after decryption of each and sends the file to requested client
 
i think this si really an exam like thing bt i felt happy after seeing your work if you can help me getiing file from clients to server for joinging
 
i have splitter and joinner i.e. ffsj.exe search in internet you will get it
i used that to split and send.exe your code to send files each split file after encrypting it
 
problem is how do i get this getting file from all clients and join them after decryption of each file
 
we can open the splitter ffsj.exe using system("c:\\ffsj.exe");
and others also same way
this is not pain its your greates achive ment i am asking you hel me get file on request thats it and reset i will do it.
GeneralsuperbmemberMember #139195622 Mar '07 - 0:25 
Superb article Smile | :)
GeneralVery perfectmembersaka-wind25 Jan '07 - 15:42 
First I want to say "Thank you"
It is my first programing of winsock,and so luckly I found your article,It is useful for me to study winsock.
 
For more, It is time for me to improve my English. Smile | :)
QuestionIP CameramemberBaalan714323 Jan '07 - 13:22 
Hi there,
I bought a D-link IP camera, I though it should display the video on my own GUI (in C++ or Java). But the IP camera is built for displaying the video on web browser. How should I bring the IP camera video display into my own GUI interface. if anyone have the answer for this question???
 

regards
Baalan
AnswerRe: IP Cameramember@netzgear@26 Mar '07 - 0:24 
Hi Baalan,
I have a IP-Cam of "MOBOTIX", using the SDK of the Camera I can easy put Video in my own applicaton(e.g. VB application ). I think you should find the SDK for your Kamera, maybe refer to the support of D-Link.
good luck!
 

QuestionWhat about accept errors?memberChrisRibe11 Jan '07 - 5:03 
Hi,
 
I noticed that you do not account for accept errors ?
I am looking to figure out what should be done when an accept error does occur...
 
I am building a server that accepts multiple socket connections and once in a while an accept error does occur. I have tried just ignoring the socket that has an error and continue accepting new connections, but I seem to be causing a memory leak. Should I be calling Close(badSocket); or some other cleanup method before continuing my accept ? Confused | :confused:
 
Thanks for any comments on this Wink | ;)
Chris
GeneralMessagesmemberjnhemley23 Nov '06 - 6:39 
I can't read a number of the messages. Some I can. What do I do ?
Generalhtonl not used when setting local.sin_addr.s_addrmembermbrezu213 Sep '06 - 22:24 
Hi,
 
nice article - one thing though - you're listening on 0.0.0.0 (INADDR_ANY).
 
When trying to listen on localhost or a specific interface/ip, I had to wrap the IP in a htonl call (probably due to the little endian nature of x86) like this:
 
local.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
 
Maybe it would be a good idea to wrap the INADDR_ANY in a htonl call (even if it doesn't do anything for INADDR_ANY) just to help people trying to bind to something else.
 
Thanks.
QuestionError While CompilingmemberMen_D_Men22 Aug '06 - 23:52 
Hi Nishant,
 
Was tring to compile your code, and I received this error:
 
------ Build started: Project: Socket_2, Configuration: Debug Win32 ------
Compiling...
stdafx.cpp
c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxv_w32.h(16) : fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include
Build log was saved at "file://d:\Visual Studio Training\Socket_2\Socket_2\Debug\BuildLog.htm"
Socket_2 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
Any idea of how to solve this??!!
 
Thanks!
AnswerRe: Error While Compilingmembercristitomi23 Feb '07 - 2:29 
You must not include the header file:
#include <windows.h>

in your project (keep in mind that this has MFC support).
You already have
#include <afxwin.h>
and that is all you need.
 
I am in love with VC++
GeneralRe: Error While CompilingmemberDavid J Turner28 Apr '07 - 2:11 
Smile | :) I found the same problem when using Visual Studio.NET 2003 (version 7), when I added my includes at the top of stdafx.h. I got a successful build by moving the includes specifed in the article to after those automaticaly generated by Visual Studio in stdafx.h.
 
Dave Turner
Sydney
Australia

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 26 Feb 2002
Article Copyright 2002 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid