青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

Beginning Winsock Programming - Simple TCP server

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 #includeconio.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)
    {
        return0;
    }

    //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 exitif(server==INVALID_SOCKET)
    {
        return0;
    }

    //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 errorif(bind(server,(sockaddr*)&local,sizeof(local))!=0)
    {
        return0;
    }

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

    //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();

    return0;
}

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.

About Nishant Sivakumar



Editor
Site Builder
Nish is a real nice guy living in Toronto who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he has moved to Toronto so he can enjoy the cold winter and get to play in some snow.

He works for The Code Project and is in charge of the MFC libraries Ultimate Toolbox, Ultimate Grid and Ultimate TCP/IP that are sold exclusively through The Code Project Storefront. He frequents the CP discussion forums when he is not coding, reading or writing. Nish hopes to visit at least three dozen countries before his human biological mechanism stops working (euphemism used to avoid the use of the d-word here). Oh btw, it must be mentioned that normally Nish is not inclined to speak about himself in the 3rd person.

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.

posted on 2006-04-18 17:41 楊粼波 閱讀(646) 評論(0)  編輯 收藏 引用 所屬分類: 文章收藏

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲国产欧美不卡在线观看| 亚洲一区二区在线免费观看视频| 久久久久久有精品国产| 欧美高清在线一区二区| 亚洲男人第一网站| 欧美天堂亚洲电影院在线播放 | 亚洲大胆人体视频| 久久噜噜噜精品国产亚洲综合| 中国女人久久久| 欧美日韩中文在线| 亚洲图片激情小说| 日韩一区二区高清| 欧美日韩天堂| 亚洲影视在线| 亚洲免费婷婷| 国产亚洲免费的视频看| 久久精品99国产精品日本| 欧美一区二区三区免费观看| 国产一区二区av| 久久久午夜电影| 久久免费精品日本久久中文字幕| 娇妻被交换粗又大又硬视频欧美| 久久免费少妇高潮久久精品99| 久久久久国产精品人| 亚洲国产精品久久久久秋霞不卡| 亚洲韩国青草视频| 欧美日韩视频在线一区二区 | 国产日韩av在线播放| 久久aⅴ乱码一区二区三区| 欧美在线视频在线播放完整版免费观看 | 91久久精品网| 欧美性视频网站| 欧美在线观看一二区| 久久久久久久久一区二区| 在线电影院国产精品| 亚洲国产精品va| 国产精品福利网| 久久婷婷综合激情| 欧美日韩成人在线观看| 久久精品国产综合| 国产日韩欧美一区在线| 一区国产精品| 亚洲国产欧美一区| 国产精品久在线观看| 巨胸喷奶水www久久久免费动漫| 欧美电影免费观看高清完整版| 中文亚洲免费| 久久久久久久97| 这里只有精品丝袜| 翔田千里一区二区| 亚洲久色影视| 欧美一区二区在线免费播放| 99re6热只有精品免费观看| 性8sex亚洲区入口| 夜夜嗨av一区二区三区四区 | 国产日韩欧美电影在线观看| 欧美激情一区在线| 国产精品天天摸av网| 亚洲电影在线观看| 国产亚洲欧美中文| 夜夜爽www精品| 亚洲黄网站黄| 欧美一区在线视频| 亚洲自拍另类| 欧美人成免费网站| 欧美国产视频日韩| 国内精品久久久久久久影视麻豆| 夜夜嗨一区二区三区| 99精品热6080yy久久| 久久综合电影| 久久一区精品| 韩国精品在线观看| 小嫩嫩精品导航| 亚洲综合三区| 欧美日韩免费区域视频在线观看| 欧美大片第1页| 国内精品视频在线播放| 午夜欧美不卡精品aaaaa| 亚洲欧美电影在线观看| 欧美日韩午夜精品| 亚洲理伦电影| 在线亚洲免费| 欧美精品久久一区| 亚洲经典三级| 日韩一级大片在线| 欧美激情麻豆| 亚洲人精品午夜在线观看| 亚洲精品久久久一区二区三区| 久久噜噜噜精品国产亚洲综合| 久久中文字幕一区| 亚洲成人在线视频播放| 另类激情亚洲| 91久久精品美女| 在线一区亚洲| 欧美体内she精视频| 亚洲午夜精品久久| 欧美在线亚洲在线| 激情亚洲一区二区三区四区| 久久久精品国产免费观看同学| 免费亚洲视频| 洋洋av久久久久久久一区| 国产精品www.| 欧美一区午夜视频在线观看| 美女久久一区| 亚洲精品乱码久久久久久久久| 欧美精品1区2区| 一区二区三区不卡视频在线观看| 久久午夜电影网| 国产精品久久久久久久久果冻传媒| 亚洲少妇最新在线视频| 欧美一区二视频| 极品日韩久久| 欧美精品1区2区| 中国成人黄色视屏| 久久阴道视频| av成人国产| 国产美女诱惑一区二区| 久久成人免费网| 亚洲高清不卡av| 午夜精品免费在线| 久久精品噜噜噜成人av农村| 欧美激情国产高清| 亚洲欧美日韩高清| 在线观看91精品国产麻豆| 欧美日本精品| 欧美在线视频在线播放完整版免费观看| 欧美成人按摩| 午夜宅男久久久| 最新日韩av| 国产精品视频专区| 欧美成人在线影院| 午夜精品在线| 亚洲精品国产无天堂网2021| 久久婷婷丁香| 亚洲一区三区视频在线观看| 黑丝一区二区| 国产精品成人一区二区| 久久久综合免费视频| 亚洲图片自拍偷拍| 亚洲第一综合天堂另类专| 久久精品二区亚洲w码| 中文日韩在线| 亚洲国产视频一区二区| 国产性色一区二区| 欧美日韩成人激情| 免费成人av资源网| 欧美一区二区三区四区在线观看地址 | 欧美国产日本| 欧美一区=区| 夜夜夜久久久| 狠狠色2019综合网| 国产精品五区| 欧美视频在线不卡| 欧美激情 亚洲a∨综合| 久久国产精品色婷婷| 在线亚洲一区| 亚洲精品一区二区三区在线观看| 蜜桃伊人久久| 久久婷婷综合激情| 久久精品久久99精品久久| 亚洲欧美精品| 亚洲图片欧美日产| 亚洲视频网站在线观看| 亚洲视频精品| 一区二区三区久久网| aaa亚洲精品一二三区| 久久影院午夜片一区| 欧美一区二区三区免费看| 亚洲欧美日韩在线观看a三区| 夜夜嗨av一区二区三区中文字幕 | 久久久蜜臀国产一区二区| 午夜精品久久久久久久蜜桃app | 久久精品国产成人| 欧美xx69| 欧美福利影院| 欧美/亚洲一区| 欧美顶级少妇做爰| 欧美91视频| 欧美成年人视频网站欧美| 欧美a级理论片| 美女黄色成人网| 亚洲乱码久久| 亚洲精品欧美一区二区三区| 亚洲国产乱码最新视频| 亚洲国产精品日韩| 一区免费观看视频| 亚洲激情在线播放| 99国产精品久久久久久久| 99在线热播精品免费| 亚洲一区成人| 欧美伊人久久| 美日韩在线观看| 亚洲国产精品免费| 亚洲精品欧美精品| 亚洲一区二区三区在线看| 欧美中文字幕精品| 欧美va亚洲va日韩∨a综合色| 欧美日本精品| 国产视频丨精品|在线观看| 女仆av观看一区|