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

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
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在线| 国产欧美日韩在线视频| 国产一区二区三区精品久久久| 国产综合在线看| 亚洲日韩欧美视频| 欧美怡红院视频一区二区三区| 免费短视频成人日韩| 亚洲美女av网站| 久久婷婷国产麻豆91天堂| 国产精品videosex极品| 黄色成人91| 亚洲免费在线视频| 亚洲国产精品一区二区久| 亚洲黄色三级| 久久久久国产精品一区| 国产精品xxxxx| 亚洲啪啪91| 免费看亚洲片| 欧美一区二区免费视频| 欧美日韩第一页| 亚洲国产一区二区视频| 久久精品国产亚洲一区二区三区| 日韩西西人体444www| 久久精品一区二区三区中文字幕| 欧美日韩在线三区| 亚洲久久成人| 欧美黄免费看| 久久色中文字幕| 国产一区二区三区免费不卡| 亚洲午夜精品一区二区| 91久久国产综合久久蜜月精品 | 亚洲精品久久视频| 久久精品论坛| 欧美亚洲在线观看| 国产欧美日韩一区二区三区在线观看 | 亚洲综合色激情五月| 欧美国产综合| 蜜臀久久久99精品久久久久久| 国产亚洲午夜| 久久精品国产第一区二区三区| 亚洲一区二区三区中文字幕| 国产精品成人国产乱一区| 亚洲一区二区视频在线| 日韩午夜精品视频| 国产精品av免费在线观看| 亚洲欧美在线x视频| 亚洲一区二区在线看| 国产精品有限公司| 久久精品国产亚洲a| 欧美中文在线视频| 国语自产精品视频在线看一大j8 | 久久亚洲精品中文字幕冲田杏梨| 国产精品综合久久久| 亚洲影院色在线观看免费| 在线视频欧美精品| 国产精品久久久久久福利一牛影视| 一区二区三区 在线观看视频 | 久久精品成人一区二区三区蜜臀| 亚洲欧美日韩国产另类专区| 国产精品系列在线播放| 欧美自拍偷拍| 久久久久久久高潮| 亚洲另类一区二区| 宅男噜噜噜66国产日韩在线观看| 国产精品视频导航| 久久久久国产精品www| 裸体女人亚洲精品一区| av成人激情| 亚洲欧美久久久| 午夜视频在线观看一区| 禁久久精品乱码| 亚洲欧洲在线观看| 国产精品成人一区二区网站软件| 久久99在线观看| 美国十次成人| 亚洲欧美大片| 久久伊人免费视频| 亚洲手机成人高清视频| 久久精品99国产精品酒店日本| 亚洲欧洲日本mm| 亚洲午夜伦理| 亚洲国产精品久久久久秋霞不卡| 亚洲精品小视频在线观看| 国产免费成人| 亚洲欧洲一区二区三区| 国产乱码精品一区二区三区忘忧草 | 欧美国产日韩视频| 欧美一区二区三区免费大片| 久久影视精品| 欧美一区成人| 欧美日韩色一区| 欧美激情第三页| 国产日本欧美在线观看| 日韩一级免费| 亚洲美女视频| 麻豆精品视频在线| 久久久999精品免费| 欧美日韩色综合| 亚洲激情国产精品| 欲色影视综合吧| 欧美伊人精品成人久久综合97| 亚洲图片激情小说| 99国产精品久久久久久久| 禁断一区二区三区在线| 校园春色国产精品| 亚洲美女黄网| 久久久99国产精品免费| 亚洲视频在线观看一区| 麻豆av福利av久久av| 久久www成人_看片免费不卡| 欧美日韩国语| 亚洲美女诱惑| 一本色道久久综合亚洲精品婷婷| 久久一区国产| 欧美成人精品福利| 亚洲福利av| 久久久久久久999| 久久综合婷婷| 一区二区三区在线观看欧美| 久久成年人视频| 久久久久国产精品厨房| 国产亚洲精品自拍| 欧美主播一区二区三区美女 久久精品人| 亚洲一区中文| 国产麻豆精品久久一二三| 亚洲午夜视频| 久久精品视频99| 在线免费精品视频| 欧美a级片网| 日韩视频免费看| 午夜精品理论片| 国产午夜精品视频| 可以免费看不卡的av网站| 亚洲第一在线| 在线亚洲欧美视频| 国产乱人伦精品一区二区| 欧美怡红院视频一区二区三区| 久久精品国产在热久久| 雨宫琴音一区二区在线| 欧美成人自拍| 亚洲视频福利| 久久综合国产精品台湾中文娱乐网| 久久久综合免费视频| 久久综合久久88| 亚洲精品欧美精品| 国产精品v日韩精品| 久久www成人_看片免费不卡| 欧美超级免费视 在线| 日韩午夜免费视频| 国产精品视频久久| 久久一区免费| 中日韩美女免费视频网址在线观看 | 国产日韩欧美| 久久综合伊人77777麻豆| 亚洲精品一区二区三区四区高清| 午夜精彩视频在线观看不卡| 国产又爽又黄的激情精品视频 | 欧美日韩国产免费观看| 亚洲男女自偷自拍图片另类| 蜜桃久久精品乱码一区二区| 一区二区日韩免费看| 狠狠久久婷婷| 欧美午夜精品一区二区三区| 久久夜色精品一区| 亚洲一区在线播放| 免费中文字幕日韩欧美| 亚洲欧美激情精品一区二区| 怡红院精品视频| 国产精品视频观看| 欧美高清视频一二三区| 国产欧美一区二区三区国产幕精品 | 亚洲一区精彩视频| 欧美成人免费全部观看天天性色| 日韩午夜av电影| 亚洲福利国产| 国产午夜亚洲精品羞羞网站| 欧美人成在线视频| 久久在线播放| 亚洲永久免费精品| 国产视频在线一区二区| 欧美精品系列| 美女爽到呻吟久久久久| 欧美主播一区二区三区美女 久久精品人 | 国产午夜精品全部视频播放| 欧美性jizz18性欧美|