【正文】
Socket Programming in C Part 1 – Introduction The purpose of this article is to show you how you can do socket programming in C. This article assumes some familiarity with the socket programming, though you need not to be expert in socket programming. There are several flavors to socket programming like client side , server side , blocking or synchronous , nonblocking or asynchronous etc. With all these flavors in mind , I have decided to break this subject into two parts. In the part 1 I will start with the client side blocking socket. Later on in the second part I will show you how to create server side and nonblocking. Network programming in windows is possible with sockets. A socket is like a handle to a file. Socket programming resembles the file IO as does the Serial Communication. You can use sockets programming to have two applications municate with each other. The application are typically on the different puters but they can be on same puter. For the two applications to talk to each either on the same or different puters using sockets one application is generally a server that keeps listening to the ining requests and the other application acts as a client and makes the connection to the server application. The server application can either accept or reject the connection. If the server accepts the connection, a dialog can begin with between the client and the server. Once the client is done with whatever it needs to do it can close the connection with the server. Connections are expensive in the sense that servers allow finite connections to occur. During the time client has an active connection it can send the data to the server and/or receive the data. The plexity begins here. When either side (client or server) sends data the other side is supposed to read the data. But how will the other side know when data has arrived. There are two options either the application needs to poll for the data at regular intervals or there needs to be some sort of mechanism that would enable application to get notifications and application can read the data at that time. Well , after all Windows is an event driven system and the notification system seems an obvious and best choice and it in fact is. As I said the two applications that need to municate with each other need to make a connection first. In order for the two application to make connections the two applications need to identify each other ( or each other39。s puter ). Computers on work have a unique identifier called . address which is represented in dotnotation like etc. Lets see how all this works in .NET. Before we go any further, download the source code attached with this article. Extract the zip file to a folder say c:\Temp you will see following two folders : Server Client In the Server folder there will be one EXE. And in the client there will be the source code in C that is our client. There will be one file called which the solution file. If you double click that your will be launched and you will see the project SocketClientProj in the solution. Under this project you will have file. Now build the code (by pressing CtrlShiftB) and run the code you will see the following dialog box: As you can see the dialog box has a field for Host IP address ( which is the IP address of the machine on which you will run the Server Application, located under Server folder). Also there is a field where you can specify port number at which the Server is listening. The server app I have provided here listens at port 8221. So I have specified port to be 8221. After specifying these parameters we need to connect to the server. So pressing Connect will connect to the server and to close the connection press Close. To send some data to the server type some data in the field near the button name Tx and if you press Rx the application will block unless there is some data to read. With this info lets now try to check the code behind this: Socket programming in .NET is made possible by the Socket class present inside the namespace. This Socket class has several method and properties and a constructor. The first step is to create an object of this class. Since there is only one constructor we have no choice but to use it. Here is how to create the socket: m_socListener = new Socket(,)。 The first parameter is the address family which we will use, in this case, interNetwork (which is IP version 4) other options include Banyan NetBios, AppleTalk etc. (AddressFam