-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
49 lines (43 loc) · 1.35 KB
/
client.cpp
File metadata and controls
49 lines (43 loc) · 1.35 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
//CLIENT
int main(void)
{
//VARIABLE DEFINITION
int PORT = 8080;
const char *IP = "127.0.0.1";
char MESSAGE[3000];
//CREATING SOCKET
sockaddr_in clientSocketAddress;
memset(&clientSocketAddress, 0, sizeof(clientSocketAddress));
clientSocketAddress.sin_family = AF_INET;
clientSocketAddress.sin_addr.s_addr = inet_addr(IP);
clientSocketAddress.sin_port = htons(PORT);
int client = socket(AF_INET, SOCK_STREAM, 0);
//CONNECTING TO SERVER
if (connect(client,(sockaddr*) &clientSocketAddress, sizeof(clientSocketAddress))>=0){
printf("Successfully connected to server\n");
}
while(true)
{
//SENDING MESSAGE TO SERVER
printf("Client: ");
std::string input;
std::getline(std::cin, input);
memset(&MESSAGE, 0, sizeof(MESSAGE));
strcpy(MESSAGE, input.c_str());
send(client, (char*)&MESSAGE, strlen(MESSAGE), 0);
printf("Waiting for server response\n");
//RECEIVING MESSAGE FROM SERVER
memset(&MESSAGE, 0, sizeof(MESSAGE));
recv(client, (char*)&MESSAGE, sizeof(MESSAGE), 0);
printf("Server: %s\n", MESSAGE);
}
close(client);
printf("Socket closed\n");
return 0;
}