0% found this document useful (0 votes)
5 views2 pages

C++ Socket Client Example Code

This C++ program demonstrates a client application using socket programming. It creates a socket, specifies the server address and port, connects to the server, sends a message, and then closes the socket. The message sent to the server is 'Hello, server!'.

Uploaded by

gunjalchetan4041
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

C++ Socket Client Example Code

This C++ program demonstrates a client application using socket programming. It creates a socket, specifies the server address and port, connects to the server, sends a message, and then closes the socket. The message sent to the server is 'Hello, server!'.

Uploaded by

gunjalchetan4041
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// C++ program to illustrate the client application in the

// socket programming

#include <cstring>

#include <iostream>

#include <netinet/in.h>

#include <sys/socket.h>

#include <unistd.h>

int main()

// creating socket

int clientSocket = socket(AF_INET, SOCK_STREAM, 0);

// specifying address

sockaddr_in serverAddress;

serverAddress.sin_family = AF_INET;

serverAddress.sin_port = htons(8080);

serverAddress.sin_addr.s_addr = INADDR_ANY;

// sending connection request

connect(clientSocket, (struct sockaddr*)&serverAddress,

sizeof(serverAddress));

// sending data

const char* message = "Hello, server!";

send(clientSocket, message, strlen(message), 0);


// closing socket

close(clientSocket);

return 0;

You might also like