Tcp ip socket programming in c windows




















It is an intermediate layer of the application layer and internet protocol layer in the OSI model. TCP is designed to send the data packets over the network. It ensures that data is delivered to the correct destination.

TCP creates a connection between the source and destination node before transmitting the data and keeps the connection alive until the communication is active. In TCP before sending the data it breaks the large data into smaller packets and cares the integrity of the data at the time of reassembling at the destination node. TCP also offers the facility of retransmission, when a TCP client sends data to the server, it requires an acknowledgment in return. If an acknowledgment is not received, after a certain amount of time transmitted data will be loss and TCP automatically retransmits the data.

In this example, After the connection with a client, the server will wait for a message from the client. After getting the message server will check the received message and send a proper response as per the received message. Today Grab your free Trial. This is how you do it :. So you need to know the IP address of the remote server you are connecting to. Here we used the ip address of google.

A little later on we shall see how to find out the ip address of a given domain name. The last thing needed is the connect function. It needs a socket and a sockaddr structure to connect to. Here is a code sample.

It cannot be any simpler. It creates a socket and then connects. If you run the program it should show Connected. Try connecting to a port different from port 80 and you should not be able to connect which indicates that the port is not open for connection. Connection means a reliable "stream" of data such that there can be multiple such streams each having communication of its own.

Think of this as a pipe which is not interfered by other data. These are non-connection based communication. Which means you keep sending or receiving packets from anybody and everybody. Function send will simply send data. It needs the socket descriptor , the data to send and its size. Here is a very simple example of sending some data to google. The message is actually a http command to fetch the mainpage of a website.

Function recv is used to receive data on a socket. In the following example we shall send the same message as the last example and receive a reply from the server. We can see what reply was send by the server. Quite simple! Function closesocket is used to close the socket. So in the above example we learned how to : 1. Create a socket 2. Connect to remote server 3. Send some data 4.

Receive a reply. Its useful to know that your web browser also does the same thing when you open www. A client is a system that connects to a remote system to fetch or retrieve data.

A server is a system that uses sockets to receive incoming connections and provide them with data. It is just the opposite of Client. So www. Or more technically www. Now its time to do some server tasks using sockets. But before we move ahead there are a few side topics that should be covered just incase you need them.

When connecting to a remote host , it is necessary to have its IP address. Function gethostbyname is used for this purpose. It takes the domain name as the parameter and returns a structure of type hostent.

This structure has the ip information. It is present in netdb. Lets have a look at this structure. So the above code can be used to find the ip address of any domain name. Then the ip address can be used to make a connection using a socket. Used by connect , send , recv etc.

Used by gethostbyname. Open a socket 2. Bind to a address and port. Listen for incoming connections. Accept connections 5. Function bind can be used to bind a socket to a particular address and port. Now that bind is done, its time to make the socket listen to connections. We bind a socket to a particular IP address and a certain port number.

By doing this we ensure that all incoming data which is directed towards this port number is received by this application. After binding a socket to a port the next thing we need to do is listen for connections. For this we need to put the socket in listening mode. Function listen is used to put the socket in listening mode. Just add the following line after bind.

So now this program is waiting for incoming connections on port Dont close this program , keep it running. Now a client can connect to it on this port.

We shall use the telnet client for testing this. Open a terminal and type. So we can see that the client connected to the server. Try the above process till you get it perfect. It is very simple :. We accepted an incoming connection but closed it immediately. This was not very productive. There are lots of things that can be done after an incoming connection is established.

Afterall the connection was established for the purpose of communication. So lets reply to the client. Run the above code in 1 terminal. And connect to this server using telnet from another terminal and you should see this :.

Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages. Stats 1. Please Sign up or sign in to vote. Download demo project - What the Heck are Threads, Ports, and Sockets? Internet Explorer etc. Each chat program varies, as there is no specific "chat" service and multiple messaging programs may run at the same time When you're sending your email, you and the remote mail server are communicating using port 25 And, when you receive email, your mail client such as Microsoft Outlook uses port to retrieve your mail from the mail server And onward extends the list.

Copy Code. Member Jul Member Apr Karlis8 Sep R ido Nov MohsenFM Nov User 9-Nov Sam L 9-May Member 2-Oct GhabXPH 4-Aug Ali Reza Barkhordari Jun Member Jun Sun of Re Apr EJP 2-Apr Question Marks 9-Jul EJP 9-Aug Dilip K Sharma Mar Go to top. Layout: fixed fluid. United States. First Prev Next. Member Awesome explanation.. Thanks a lot!! Multi conection int a FIFO structure? Your code works nice, now i need to apply it in a First In First Out structure, i'm wondering if this code could allowing multi conection to the same listener as server and the server resend to many conections, as a chat.

Great post! It's clearly explained. RECV Function is waiting for a long time.. Pls help aruzhere Mar I have set up the socket and connection to the socket has been established. But when the execution of program leads to the recv funtion; it is waiting forever. Please help. My vote of 3 Karlis8 Sep Gives decent overall picture, but content about listen and send is very misleading.

Excellent article! Thank you for explaining socket programming so clearly! Can we get a. That will be very helpful. Re: sln file parikshitnangre Nov Also there is a password for the word file in zip folder. What is the password?

My vote of 5 R ido Nov Great explanation, thanks, you've saved me a lot of time! My vote of 5 User 9-Nov User I'm late But your article helped me to understand the Basics. Thank you.

Bruno modified Jan pm. My Vote Sam L 9-May Much helpful article for Socket programming novice, Thank you. My vote of 3 Member 2-Oct Title says all. Excellent article kamleshj 1-Aug Thanks for posting this article. Explained very nicely and easy to understand even for beginners. My vote of 5 Ali Reza Barkhordari Jun Ali Reza Barkhordari. Thanks very much,This article is what that i am looking for. Error while copying code to Codeblocks Sun of Re Apr Hi all, I am getting the following error when trying to run the code.

I have only copied the code up to, "Before you move on, type this code up and give it a try. Extrremely unreliable article. You will quickly find out when you attempt it that your program will freeze until an incoming connection is made No, you won't find that out. Your program will not freeze at that point. No it isn't. This is definitely a problem No it isn't. You can then specify, with the next and final parameter, how many remote computers can communicate with your server at the same time No you can't.

That's not what the parameter is for. It suggests a value for the size of the backlog queue. No we don't, not unless we want to drive the kernel crazy and waste huge amounts of memory. The usual size for the backlog queue is a few hundred. Complete and utter nonsense. Obviously you never even tried it. Because using blocking functions such as listen is so impractical It isn't a blocking function and it isn't impractical.

I'm obliged to keep repeating myself here because you have repeated the error so often that it requires attention called to it. A moment's thought should have revealed to you that this implies that listen does not block.



0コメント

  • 1000 / 1000