We help IT Professionals succeed at work.
Get Started

Winsock programming: program a client/server TFTP server

Highlord101
Highlord101 asked
on
3,370 Views
Last Modified: 2012-06-27
Here is my code for client and server model using winsock programming in VC++, I included the wsock.lib and ws2_32.lib.  Since TFTP is a UDP protocol, so server does not need to "listen" nor "accept" connection, all it have to do with waiting for client to send a data.  However, my program won't talk to each other, eventhough both code compile and run fine.  This is a really simple client/server model, which I eventually will turn into a TFTP server/client.  All I need to do is to make them talk to each other for now.  I have written a TCP model, and it works fine, but since no connection is established in UDP server, i don't really know how they see each other.
In addition, is there any source code available for refrence on writing a TFTP server/client?

 Here is my code:

Server:

#include <stdlib.h>
#include <iostream>
#include <string>
#include <winsock.h>
using namespace std;

int initialize_server()
{
      SOCKET sock1, sock2;  //sock1 for listening, sock2 for connection
      bool end = false;
      sock1 = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
      
      if(sock1 == INVALID_SOCKET)
      {
            cout << "Cannot Create Socket " << endl;
            return 0;
      }

      struct sockaddr_in serAddr, recAddr;
      int result, len = sizeof(recAddr);            
      memset((void*)&serAddr,0,sizeof(serAddr));
      serAddr.sin_family = AF_INET;
      serAddr.sin_addr.s_addr = htonl(INADDR_ANY);
      serAddr.sin_port = htons(69);   //need to modify to TFTP port

      result = bind(sock1, (struct sockaddr*)&serAddr,sizeof(serAddr));

      if(result == SOCKET_ERROR)
      {
            cout << "Cannot bind the socket " << endl;
            cout << "The error number is " << WSAGetLastError() << endl;
            closesocket(sock1);
            return -1;
      }

            

      char recieved[256];
                memset((void*)recieved, 0, sizeof(recieved));
                while(!end)
      {
            recv(sock2, recieved, sizeof(recieved), 0);
      }
      
      return 0;
}

int main(int argc, char* argv[])
{
      int t;
      WSADATA wsa_data;
                t = WSAStartup(MAKEWORD(1,1), &wsa_data);
      
      if(t != 0)
            cout << "WSAStartup() fails" << endl;

      initialize_server();
      t = WSACleanup();
      if(t == SOCKET_ERROR)
      {
            cout << "Cannot sign off from the Window Sockets API " << endl;
            cout << "The error number is " << WSAGetLastError() << endl;
      }
               return 0;
}

Client:
#include <iostream.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>


int initiate_client(const char* serAddr)
{
      SOCKET sock1 = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
      bool end = false;

      if(sock1 == INVALID_SOCKET)
      {
            cout << "Cannot create socket" << endl;
            cout << "The error is " << WSAGetLastError() << endl;
            return -1;
      }

      struct sockaddr_in Addr;
                Addr.sin_addr.s_addr = inet_addr(serAddr);
      Addr.sin_family = AF_INET;
      Addr.sin_port = htons(69);

      int result;
                result = connect(sock1,(sockaddr*)&Addr,sizeof(struct sockaddr_in));
      if(result == SOCKET_ERROR)
      {
            cout << "cannot connect to the server" << endl;
            cout << "The error is " << WSAGetLastError() << endl;
      }
      
      printf("connected.\n");

         char data[] = "This is Server";
                while (!end)
      {
            send(sock1, data, strlen(data), 0);
      }

      closesocket(sock1);
      return 0;
}

int main(int argc, char* argv[])
{
      int t;
      WSADATA wsa_data;
      WSAStartup(MAKEWORD(1,1), &wsa_data);
      
      t = initiate_client(argv[1]);      
      
      t = WSACleanup();
      if(t == SOCKET_ERROR)
      {
            cout << "Cannot sign off Window Socket API .dll" << endl;
            cout << "The error is " << WSAGetLastError();
      }
}


            
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE