-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
30 lines (24 loc) · 702 Bytes
/
client.cpp
File metadata and controls
30 lines (24 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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;
}