Client socket AF_INET

 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
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(9734);
int len = sizeof(address);
int result = connect(sockfd, (sockaddr*)&address, len);
if (-1 == result) {
std::cout<<"Can not connect to server socket"<<std::endl;
}
char ch = 'A';
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
std::cout<<"Char from server: "<<ch<<std::endl;
close(sockfd);
return 0;
}