Answer the question
In order to leave comments, you need to log in
How to make a TCP server in C++ for Ubuntu?
The fact is that I'm trying to understand the server code on sockets, and along the way I'm writing my own server, but when I'm trying to connect, I can't
recreate what is here (from the bottom of the EXO server, well, at least I just connect ...)
https://rsdn. org/article/unix/sockets.xml
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
int i = 0;
int sock, listener;
struct sockaddr_in addr;
char buf[1024];
int bytes_read;
listener = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(1300);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
listen(listener,1);
sock = accept(listener, NULL, NULL);
bytes_read = recv(sock, buf, 1024, 0);
if ( bytes_read != 0 )
{
while( i == bytes_read )
{
cout << buf[i];
i = i + 1;
}
}
close(sock);
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question