T
T
Type Programmer2018-03-19 20:52:18
C++ / C#
Type Programmer, 2018-03-19 20:52:18

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;
}

PS: I'm trying to climb over to Linux, I'm not good at programming, I'm also bad with Russian...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2018-03-19
@MegaCraZy6

You forgot to bind
https://www.binarytides.com/server-client-example-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question