TCP/IP File Transfer

Posted on 2003-03-03
Medium Priority
Last Modified: 2010-05-18
I'm looking to build a simple program that does a couple rather simple things:

1. determines the IP address of the computer that it is run on.
2. Act either as a server or a client, and allow files to be sent between two computers both running the program.

I've been looking at sockets etc.

Can someone give me some information on how to do this, as well as some sample code if possible?

This needs to work in MSVC++ 6.

Thanks =)
Question by:birdgenj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

d_iego earned 300 total points
ID: 8061925
I think this could help, it is an example of socket client and server implementetion.  Its not mine, but it helped me a lot.

Submitted on: 8/24/2001 2:09:03 PM
By: Jason Beighel  
Level: Beginner
User Rating:      By 20 Users
Compatibility:C++ (general), Microsoft Visual C++

Users have accessed this article 14372 times.
  (About the author)
     This article is my understanding of how to use the WinSock API. It explains how to create a socket, listen on a socket as a server, connect to a socket as a client, and how to pass information from the client to the server.

Terms of Agreement:  
By using this article, you agree to the following terms...  
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.  
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.  
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.  
First and foremost in order to use the Winsock API you have to link to the libraries mpr.lib and wsock32.lib. To do this in Visual Studio create a new project then under the "Projects" menu choose "Settings...", or just hit Alt+F7. In the top left of the dialog box there is a drop down list box labeled "Settings For:" change it to read "All Configurations". In the tab control on the right of the dialog box select the "Link" tab. In the middle of the tab there is an edit box labeled "Object/Library Modules:" add the name of the libraries you want to link to, be sure all the labraries in the list are separated by spaces. That being done you can now begin to program.

The first step in using the WinSock API is to initialize WSA. I'm not positive what WSA is, I'm assumng its short for WinSockApi, but I can't back that up. Whatever it is it has to be initilized. This is done by calling WSAStartup(). This function takes two parameters a version number in a WORD value and a WSADATA structure, it returns an integer the return will be 0 if initialization is successful. Here is an example of the initialization process:

     WSADATA WsaDat;
     if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
            printf("WSA Initialization failed.");

For the version number I use the macro MAKEWORD(). It splits the version number up and its easy to see what you are requesting. When you send that version number you are requesting a specific version of WinSock, in the example I am requesting version 1.1. You can request version 1.0, 1.1, and 2.0, version 2.0 is not available in Win 95 without being specifically installed it does exist in all later versions of Windows. The exact benifits of each version I'll leave to you to research, from what I have read version 1.1 has all the important features and since its available in all version of Windows without a patch it is acceptable for most applications.

After you have initialized WinSock the next step is to create a socket. Sockets are of two types stream sockets and datagram sockets. Stream sockets are easier to use so I'll demonstrate them. All sockets are of type SOCKET, and you create them with the socket() function. The socket() function takes three parameters. The first is the type of connection you would like to use, for this use AF_INET this designates you want to use an Internet style connection (or in other words use TCP/IP) as far as I know this is the only connection permitted through WinSock. The second parameter is the type of socket to use, for stream sockets use SOCK_STREAM, or for datagram sockets use SOCK_DGRAM. The thrid parameter is some value for the protocol from what I have read this value has very little meaning and is usually ignored so I always pass zero here. The socket() function will return the socket or INVALID_SOCKET if it can't create the socket. Here is an example of that:

      SOCKET Socket;
      Socket = socket(AF_INET, SOCK_STREAM, 0);
      if (Socket == INVALID_SOCKET)
          printf("Socket creation failed.");

Now we have a usable socket, what we need to do is make use of it. As with any network connections you have to have a server and a client. For clarity I'm going to call the server the computer that is listening for and incoming connection and the client the computer that requests a connection with a server. Since the server has to be listening before a client can connect I'll show how to setup the server first. First we bind the socket to a TCP/IP port. This is done with the bind() function. The bind() function takes three parameters, a socket to bind to, a pointer to a data structure that has the port information (structure type STRUCTADDR), and the size of the structure with the port information. There are a few points of interest in this process so i'll just explain inside an example.

//The variables we will need SOCKADDR_IN SockAddr;
//We need a socket variable but for now // lets assume its the variable Socket we prepared before.
//bind() does require one of those prepa // red sockets, so at one point you will need to create one.
/* For those who are paying attention you may have noticed that I said before that we need a struct SOCKADDR variable. Except I didn't declare one here. The reason is that struct SOCKADD_IN holds the same information in the same way as struct SOCKADDR does, the difference is that struct SOCKADDR_IN is easier to work with. */

//We want to use port 50
SockAddr.sin_port = hons(50);

//We want an internet type connection (TCP/IP)
SockAddr.sin_family = AF_INET;

//We want to listen on IP address
//I'll give a few better ways to set thi // s value later
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 1;

//Ok all the information is set, lets bind()
if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
      printf("Attempt to bind failed.");

That ought to be fairly straight forward to figure out. The connection type should always be AF_INET, the port is an unsigned integer between 0 and 65,565, and the address is four unsigned short values from 0 to 255 that is a valid IP address of the server. We can specify the IP address we want to listen to, what if we want to listen on multiple addresses? You could run throuh this process multiple times to bind a socket on each address, or you could set the SockAddr.sin_addr.S_un.S_addr to INADDR_ANY like this:

SockAddr.sin_addr.S_un.S_addr = INADDR_ANY;

Instead of setting the four octets of an IP address. The next issue that comes up would be how do I know my IP address? There is a way of finding the address, but its a little involved so I'm going to discuss that later. Now that we have a valid socket bound to a TCP/IP port we need to listen on that socket for incoming connections. We use the listen() function to accomplish that. The listen() function takes two parameters a bound socket and the number of connections to accept. Here is how that looks:

//Once again we're carrying through the Socket variable from the previous example.
//We're only going to accept 1 incoming // connection.
listen(Socket, 1);

Not much to listen(). Just to clarify the listen() function does not accept the incoming connections, it just sets your socket to listening on the specified port, no more no less. To accept the incoming connection you use accept(). The accept() function will will watch the port for a breif time then return an error. So unless you know exactly when the connection is coming and can start accept at just the right time you are going to miss the connection. One way around this is to place accept() in a while loop until a connection is received. There is a problem with this technique, in a DOS or console application its fine since nothing else can be happening it doesn't matter, but in a windows program it will stop responding until it gets out of that loop. You may be able to set the accept() function to run on a short timer or in a loop that is called in a thread. At any rate here is how it would look if it were in a while loop until it received a connection:

//We are still carrying through the Socket variable from before
while (TempSock == SOCKET_ERROR)
      TempSock = accept(Socket, NULL, NULL);
Socket = TempSock;

The reason for creating the TempSock variable is to preserve our real socket. I don't want to overwrite it with an error just because we missed a connection. I never looked into what is returned on a successful connection, I would assume it is the socket you started with, but from examples I looked at it doesn't appear to do that. All the documentation I read on accept() skipped over the return value, they just copied the results back into the original socket so I am doing the same. The second two parameters can be used to gain information on who connected by passing a pointer to a SOCKADDR structure and its size like this:

accept(Socket, &Addr, sizeof(Addr);

I never tested sending a SOCKADDR_IN the same as in bind() but I haven't tested this so I won't gaurentee the results of this.
So now we are listening on a TCP/IP port and ready to accept a connection. So lets look into requesting a connection. To do this we use the connect() function. This function takes the same parameters as the bind() function except the port and address are the ones you want to connect to instead of listen on obviously. The connect() function will return a 0 if successful. Here is an example of that:

//The variables we will need

//We need a socket variable but for now lets assume its a variable Socket we prepared earlier.
//We want to use port 50
SockAddr.sin_port = htons(50);

//We want an internet type connection (TCP/IP)
SockAddr.sin_family = AF_INET;
//We want to connect to the IP address 1 //
//I'll give a few better ways to set thi // s value later
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b4 = 1;

if (connect(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) != 0)
      printf("Failed to establish connection with server.");

Now that we have a server with a connected client they need to exchange information. This is done exactly the same for the client as it is for the server. The functions to use are send() and recv(). They both take four parameters the socket to send on, the data to send, and the number of bytes in the data. The way they expect the data is in a pointer to a char. You can bundle other values into this just typecast it into a char * and pass the correct number of bytes. The fourth parameter isn't used so give a zero there. These functions will return the number of bytes send or received if successful. They will return 0, WSAECONNRESET, or WSAECONNABORT if the connection was closed at the other end. These functions will also return SOCKET_ERROR if some error occurs during the transmission. They recv() function, like the accept() function, only watches for a brief period for the data to come through. Once again I place the function in a while loop until data is received. Here is how the recv() function looks in such a loop:

int RetVal = SOCKET_ERROR;
char String[50];

while (RetVal == SOCKET_ERROR)
      RetVal = recv(Socket, String, 50, 0);
      if ((RetVal == 0)||(RetVal == WSAECONNRESET)||(RetVal == WSAECONNABORT))
           printf("Connection closed at other end.");

Since errors are possible in sending the data I place it in a while loop as well. Here is how that looks:

int RetVal = SOCKET_ERROR;
char String[] = "Hello";
while (RetVal == SOCKET_ERROR)
      RetVal = recv(Socket, String, strlen(String) + 1, 0);
      if ((RetVal == 0)||(RetVal == WSAECONNRESET)||(RetVal == WSAECONNABORT))
           printf("Connection closed at other end.");

In these examples the data to send, or the received data is in the character array String. When the data is received there is a fixed amount of data that can be received so it is possible to overrun the buffer. That is a quick run through of how to use WinSock for network communications.

Now as I said before there are ways of determining your own network address. This is by calling gethostname(). This will not return your IP address, only the text computer name. This function takes two parameters a character array to place the computer name in and the number of characters you have allocated in that array. Here is how it looks:

char Name[255];
gethostname(Name, 255);

If you look at the example above you'll note that it uses the IP address, not the computer name. What you can do is to call gethostbyname() which will give you information about a host based on its name. It takes only one parameter, the string that has the computer name, and it returns a pointer to a HOSTENT structure. Here is an example:

HOSTENT *HostInfo;
HostInfo = gethostbyname("computer");
if (HostInfo == NULL)
      printf("Attempt to retreive computer information failed.");

The gethostbyname() function will search through DNS records in order to find the IP address. The careful readers will note that this HOSTENT structure is still worthless since it doesn't fit into the SOCKADDR_IN anywhere. The IP address is in the HOSTENT structure, its just buried. Here are the members of the HOSTENT structure that I found useful. The h_addrtype member holds the type of address this uses, as with the sockets the only type is AF_INET. The h_name is a character array that will contain the complete host and domain name for that computer, for instance host.domain.com. One catch to this, it will not do reverse name lookups, for example if you look up the computer name "MyComputer" h_name will hold "MyComputer.MyDomain.com" , however if you look up the computer named "" (which is really its IP) it will not translate that into a computer name gethostbyname() will just put the text "" in h_name. The last member I want to discuss is h_addr_list, this one is somewhat confusing so of course it has the information we are really after. The member h_addr_list if a variable of type char**, but every time I have used it only one dimension of the array is used. In the data that is filled the first four bytes hold the four octets of the IP address. The rest of the array holds the same information as h_name. The octets are written as unsigned char values so you would have to place them into the SOCKADDR_IN structure like this:

HOSTENT *HostInfo;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)HostInfo->h_addr_list[0][0];
SockAddr.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)HostInfo->h_addr_list[0][1];
SockAddr.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)HostInfo->h_addr_list[0][2];
SockAddr.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)HostInfo->h_addr_list[0][3];

In that way you can use the computer's name to find its IP so you can connect to any server you have the name of. Using this same technique you can also find your own computers IP address.

I hope this helps
Diego Mendieta

Expert Comment

ID: 8062846
nice pice of code, very nice

Author Comment

ID: 8068337
I'm getting errors:

C:\WinSockAPI\WinSockAPI.cpp(11) : error C2065: 'WSADATA' : undeclared identifier
C:\WinSockAPI\WinSockAPI.cpp(11) : error C2146: syntax error : missing ';' before identifier 'WsaDat'
C:\WinSockAPI\WinSockAPI.cpp(11) : error C2065: 'WsaDat' : undeclared identifier
C:\WinSockAPI\WinSockAPI.cpp(12) : error C2065: 'WSAStartup' : undeclared identifier
C:\WinSockAPI\WinSockAPI.cpp(12) : error C2065: 'MAKEWORD' : undeclared identifier
C:\WinSockAPI\WinSockAPI.cpp(21) : fatal error C1004: unexpected end of file found

Is there an #include file that I'm missing??
Or does the linking to the libraries supposedly solve that problem?

Author Comment

ID: 8068408
#include <afxsock.h>

It works now... =)

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question