Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open and use a socket in C? [closed]

I would like to know the simplest and most effective way to open and write data to a socket in the C programming language for network programming.

like image 429
The.Anti.9 Avatar asked Nov 21 '08 03:11

The.Anti.9


People also ask

Does Ctrl C close sockets?

When you are closing the client using ctrl + c , OS will stop the client process and close all the sockets opened by it.

What is socket () bind () listen () accept () and connect ()?

The steps involved in establishing a TCP socket on the server side are as follows: Create a socket with the socket() function; Bind the socket to an address using the bind() function; Listen for connections with the listen() function; Accept a connection with the accept() function system call.


2 Answers

You're right, using sockets in C has a difficult syntax. Later languages like Java and Python make it a snap by comparison. The best tutorial I've found for doing socket programming in C is Beej's Guide to Network Programming. I recommend you start at the beginning to get a good overview, but if you just need to get some code working now, you can skip ahead to the section titled Client-Server Background.

Good luck!

like image 179
Bill the Lizard Avatar answered Oct 06 '22 11:10

Bill the Lizard


You don't mention what platform you are on, but a copy of Unix Network Programming by Stevens would be a good addition to your bookshelf. Most operating systems implement Berkley Sockets using socket, bind, connect, etc.

like image 34
Brian C. Lane Avatar answered Oct 06 '22 12:10

Brian C. Lane