Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Nov 27th, 2005, 10:30 PM   #1
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Network programming

I found a socket tutorial at this site... tutorial and decided i wanted to test out network programming.. so my first goal was to convert it mostly into C++, so now thats done and im just posting here for people to test it as it "grows up". Please feel free to drop a message in on the server by compiling this code, then follow the instructions bellow.

 #include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <strings.h>
#include <iostream.h>

#define MAXPENDING 5
#define BUFFER_SIZE 1024
#define EXIT_CALL " exit"

int main()
{
     int local_socket = 0, remote_socket = 0, menu_switch = 0, message_length = 0, remote_length = 0;
     unsigned short local_port = 0, remote_port = 0;
     struct sockaddr_in local_address, remote_address;
     WSADATA wsa_data;
     char message[BUFFER_SIZE], remote_ip[32];

     cout << "Send data to server[1]\n";
     cout << "Listen for data as server[2]\n";
     cout << "Make your selection: ";
do {
        cin.clear ();
        if (cin.sync ());
        cin >> menu_switch;
        if (!cin.good ()) cout << "You must enter an integer: ";
        } while (menu_switch == 0);


     cout << "Enter local port to use: ";
do {
        cin.clear ();
        if (cin.sync ());
        cin >> local_port;
        if (!cin.good ()) cout << "You must enter a port: ";
        } while (local_port == 0);

     if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0)
     {
          cout << "WSAStartup() failed\n";
          return (1);
     }

     if ((local_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
     {
          cout << "socket() failed\n";
          return(1);
     }

     /* Construct local address structure */
     memset(&local_address, 0, sizeof(local_address)); /* Zero out structure */
     local_address.sin_family = AF_INET; /* Internet address family */
     local_address.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
     local_address.sin_port = htons(local_port); /* Local port */

     /* Bind to the local address */
     if (bind(local_socket, (struct sockaddr *) &local_address, sizeof(local_address)) < 0)
     {
          cout << "bind() failed\n";
          return(1);
     }

     switch (menu_switch)
     {
          case 1:
               cout << "Enter server IP: ";
do {
        cin.clear ();
        if (cin.sync ());
        cin >> remote_ip;
        if (!cin.good ()) cout << "You must enter an ip: ";
        } while (remote_ip == 0);
fflush(stdin);
               cout << "Enter server Port: ";
do {
        cin.clear ();
        if (cin.sync ());
        cin >> remote_port;
        if (!cin.good ()) cout << "You must enter a port: ";
        } while (remote_port == 0);
fflush(stdin);
               if ((remote_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
               {
                    cout << "socket() failed";
                    return(1);
               }

               memset(&remote_address, 0, sizeof(remote_address)); /* Zero out structure */
               remote_address.sin_family = AF_INET; /* Internet address family */
               remote_address.sin_addr.s_addr = inet_addr(remote_ip); /* Server IP address */
               remote_address.sin_port = htons(remote_port); /* Server port */

               if (connect(remote_socket, (struct sockaddr *) &remote_address, sizeof(remote_address)) < 0)
               {
                    cout << "connect() failed";
                    return(1);
               }

               do
               {
                    memset(&message, 0, BUFFER_SIZE);
                    cout << "Enter message to send: ";
                    gets(message);
                    fflush(stdin);

                    message_length = (strlen(message));

                    if (send(remote_socket, message, message_length, 0) != message_length)
                    {
                         cout << "send() failed";
                         return(1);
                    }
               }
               while(strcmp(message, EXIT_CALL));
          break;
          case 2:
               /* Mark the socket so it will listen for incoming connections */
               if (listen(local_socket, MAXPENDING) < 0)
               {
                    cout << "listen() failed\n";
                    return(1);
               }

               remote_length = sizeof(remote_address);
               for(;;)
               {
                    /* Accepts an incoming connection when one is detected */
                    if((remote_socket = accept(local_socket, (struct sockaddr *) &remote_address, &remote_length) ) < 0)
                    {
                         cout << "accept() failed\n";
                         return(1);
                    }
                    else
                    {
                         break;
                    }
               }

               do
               {
                    memset(&message, 0, BUFFER_SIZE);
                    /*receive the message from the client*/
                    if((message_length = recv(remote_socket, message, BUFFER_SIZE, 0)) < 0)
                    {
                         cout << "recv() failed\n";
                         return(1);
                    }
                    cout << "From client " << inet_ntoa(remote_address.sin_addr) <<": " << message << endl;
               }
               while(strcmp(remote_ip, EXIT_CALL));
          break;
     }

     WSACleanup(); /* Cleanup Winsock */
     closesocket(local_socket);
     closesocket(remote_socket);
     system("pause");
     return(0);
}

1. Compile then run the program
2. Send data to server[1]
3. Enter Local port to use: 51
4. Enter server IP: 70.80.252.72
5. Enter server Port: 50
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes