Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket Programming in C++

Can anybody provide me some sample example on Client and server connection using sockets in C++. I have gone through some tutorials now i want to implement it. How to start ?

like image 944
Swapnil Gupta Avatar asked Aug 18 '10 04:08

Swapnil Gupta


People also ask

What is socket programming in C?

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.

What is socket programming used for?

Socket programming shows how to use socket APIs to establish communication links between remote and local processes. The processes that use a socket can reside on the same system or different systems on different networks. Sockets are useful for both stand-alone and network applications.

What is type socket programming?

Socket types define the communication properties that are visible to a user. The Internet family sockets provide access to the TCP/IP transport protocols. The Internet family is identified by the value AF_INET6, for sockets that can communicate over both IPv6 and IPv4.

How do I run a socket program in terminal?

Bind socket to a port It needs a sockaddr_in structure similar to connect function. int socket_desc; struct sockaddr_in server; //Create socket socket_desc = socket(AF_INET , SOCK_STREAM , 0); if (socket_desc == -1) { printf("Could not create socket"); } //Prepare the sockaddr_in structure server.


2 Answers

You can find a working client-server program here: Beej's Guide to Network Programming

like image 78
codaddict Avatar answered Oct 17 '22 08:10

codaddict


There is no socket API in the C++ Standard. The POSIX C API is fairly portable (the GNU libC documentation provides examples of UDP and TCP clients and servers that I usually turn to when I'm scratching together another server), or you could use the Boost.ASIO library for a more C++ experience....

like image 28
Tony Delroy Avatar answered Oct 17 '22 08:10

Tony Delroy