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 ?
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.
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.
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.
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.
You can find a working client-server program here: Beej's Guide to Network Programming
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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With