Don't understand Sockets (Linux)

Hello

I read three books about sockets but I just don't understand them.
This is what I know after reading those books:
-That you can compare sockets with a phone call (getting the phone number, dig in the number, accepting the call,...)
-That you can shutdown the connection on different ways (0,1,2)
-That the first variable must be the same in those function (socket id)
-You have to include two libraries (sys/socket.h & sys/types.h)

But that's it!

I still don't know how to make a good normal connection to something.
Could someone please make a very basic code to connect to a FTP-server and/or Proxy?

1
2
3
4
5
6
7
8
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>

using namespace std;
int main() {
//And then?
}


I'd really appreciate to see an example of how to do it (I'm NOT copy/pasting), but I don't get anything from those books. :/

Thanks for reading,
Niely
http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html

I think this was the guide i used years back.

What are the three books you've read?
Last edited on
This is one of them:
http://parsys.eecs.uic.edu/~solworth/sockets.pdf

The other two do I have on an USB (will post them soon).
Technically this is written in C, but I have found this to be a good tutorial as well: http://www.linuxhowtos.org/C_C++/socket.htm

I've integrated this code into my C++ projects without any problems.

The socket stuff can be a bit daunting though, so I've written some classes to handle all of the complicated stuff so it's a bit easier to use in the rest of my code.

ie. To create a listening server socket, my serverSocket class has the following constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
serverSocket::serverSocket() {
  starting_port = 40000;
  ending_port = 50000;
  connected_port = starting_port;
  
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(connected_port);
  
  while((bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) && connected_port < ending_port) {
    connected_port++; 
    serv_addr.sin_port = htons(connected_port);
  } 
  
  if(connected_port >= ending_port) {
    cerr << "ERROR on binding" << endl;
  } else {
    cout << "Connected on port " << connected_port << endl;
    listen(sockfd, 5);
  } 
} 


The private data members for the class are:

1
2
3
4
5
6
7
8
  private:
    int                 sockfd;
    socklen_t           clilen;
    struct sockaddr_in  serv_addr;
    struct sockaddr_in  *cli_addr;
    int                 starting_port;
    int                 ending_port;
    int                 connected_port;


Along with some other structures to handle multiple connections.

That webpage also has a simple example client.c file showing how to create a client socket.
^Thanks! :)
I'll take a look at that tutorial then. :)

So, If I want to connect to something, only connect.
I only have to use socket and connect function?
If you are connecting to an already existing server, then you only need to look at the client.c example code. You will also need to be familiar with the protocol of the server you're connecting to.
Is it actually even possible to make a ftp connector?
So a user just gives in their servers IP and port so they can connect to their website's server to maintain it? :/
Of course it is possible to connect to a FTP server.
FTP clients do it.

FTP connections require two sockets. One for the control connection and one for the actual data transfer. You need to understand what goes over each socket and when. i.e. the protocol.

Since you're trying to connect to an FTP server, you might find it easier to use an existing FTP library that you bind into your program. You simply make calls to the API to connect, change directories, get or put a file, disconnect, etc. Here are some libraries to consider:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa385331%28v=vs.85%29.aspx
http://sourceforge.net/projects/libftp/
http://www.codeproject.com/Articles/8667/FTP-Client-Class
http://curl.haxx.se/libcurl/

Edit: Sorry. Just noticed your title said Linux. The MSDN link is Windows only. The others should be multi-platform.
Last edited on
Thanks! :)
Will help me further for sure. ;)
Topic archived. No new replies allowed.