Thread: Sockets

  1. #1

    Sockets

    Hey all, haven't been to the board in awhile but I have a question on sockets.

    I've made a server where a telnet client can telnet to on port 3550, everything works fine. Now I want to extend it so it logs all activities so I made it create a file and put in it:

    IP:[ip] connected to the server.

    One thing that doesn't work is the [ip] part.

    Code:
    inet_ntoa(client.sin_addr.s_addr);
    Isn't this supposed to go from machine order to usual ip number with dots?

    Well I did this and it gives me an error saying:

    Code:
    conversion from 'u_long' to non-scalar type 'in_addr' requested
    Second thing I want to add to the logging is date and time but I've never found out how to use those functions so if someone could help me out on that that would be great!

    Thanx very much for any help you may be able to offer me.

    [Devouring One]
    Last edited by devour89; 05-29-2003 at 03:37 AM.
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  2. #2

    Unhappy

    No one? Not even how to just display date and time?
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Greetings,

    For the IP part try:
    Code:
    inet_ntoa(client.sin_addr);
    'inet_ntoa' takes the structure as a parameter not the '.s_addr' member of the structure.

    Here's an example for the date/time thing:
    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    {
      time_t T;
      time(&T);
      tm *local_time = localtime(&T);
    
      char now[41];
     
      strftime(now,40u,"%a %b %d %H:%M:%S %Y",local_time);
    
      cout << now;
    }
    It outputs something like:
    Thu May 29 12:22:27 2003

  4. #4
    Ok, thanx, the Ip part works now only the time gives me two errors.

    1. invalid conversion from 'const char' to 'unsigned int'
    2. cannot convert 'tm*' to 'const char' for argument '3' to size_t

    I copied exactly what you showed me in your last post.

    [Devouring One]
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  5. #5
    Also another thing I would like to know.

    I made the server wait to recieve something but imidietly after I press any key on the client side it shows up on the server. This is partially correct as I want it to recieve a command. But what it has to do is wait for the whole command to come in one go so if in the client side i type exit and then press enter it comes as a whole on the server side as exit and then finally exits.

    I know its possible and I've been fooling around with the source code but it isn't working
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Greetings,

    I don't know what's wrong with the time functions, it works perfectly here. I'm using MS VC7 compiler, but the code is pretty standard, i think!

    Are you sending every keypress on the client to the server?
    In the client code, only send the command to the server after the user has pressed the ENTER key, maybe something like:
    Code:
    ...
    char command_buffer[2048];
    ...
    <loop>
        cin.getline(command_buffer, 2048, '\n');
        int error = send(<SOCKET>, command_buffer, 2048, 0);
       <error handling>
    <endloop>
    If you post your code it'll make it easier to help you.

  7. #7
    Thanx! It works now after I press enter. Dope I could have thought of it myself with the cin.getline. Ohh well.

    How does the server recieve the message if I send the buffer. Is it exit\n or is exit or is exit\0

    Cause if I'm not mistaken I'll have to compare two strings for it to take a command.

    Thanx Again

    [Devouring One]

  8. #8
    Thanx for the quick reply.

    Code:
    #include <winsock2.h>
    #include <iostream.h>
    #include <windows.h>
    #include <fstream.h>
    #include <time.h>
    #include <string.h>
    #define PORT 3550   /* Port that will be opened */ 
    #define BACKLOG 1   /* Number of allowed connections */
    #define MAXDATASIZE 100
    using namespace std;
    
    struct sockaddr_in server; /* server's address information */
    struct sockaddr_in client; /* client's address information */
    const int req_winsock_ver = 2;
    int sin_size;
    int numbytes;
    char buf[MAXDATASIZE];
    WSADATA  wsdata;
    SOCKET   fd, fd2;
    DWORD    wsock;
    //time_t T;
    int main()
    {
    WSAStartup(MAKEWORD(req_winsock_ver, 0), &wsdata);
    
    
       if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )  /* calls socket() */
       {
             cout<<"Socket error";
             return -1;
       }
    
          	      	server.sin_family      	= AF_INET;         
          	      	server.sin_port      	= htons(PORT);          
          	      	server.sin_addr.s_addr      = INADDR_ANY;       /* INADDR_ANY puts your IP address automatically */
    
       if(bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr)) == -1) /* calls bind() */
       { 
             cout<<"Bind Error";
                return -1;
       }    
    
    
       if(listen(fd,BACKLOG) == -1) /* calls listen() */
       { 
             cout<<"Listen Error";
                return -1;
       }
    
    
          while(1)
          {
          sin_size = sizeof(struct sockaddr_in);
          if ((fd2 = accept(fd,(struct sockaddr *)&client,&sin_size)) == -1) /* calls accept() */
          
          {
    
             cout<<"Accept Error";
             return -1;
    
          }
            
             cout<<"Client connected to server.\n";
             //time(&T);
             //tm *local_time = localtime(&T);
             //char now[41];
             //strftime(now,"%a %b %d %H:%M:%S %Y",local_time);
             ofstream a_file("data.txt", ios::app);
             a_file<<"IP: "<<inet_ntoa(client.sin_addr)<<" connected to server at:";//<<now<<"\n";
             a_file.close();
             send(fd2,"Welcome to my server.\n",22,0); /* send to the client welcome message */
             while(1)
             {
                numbytes = recv(fd2,buf,MAXDATASIZE,0);
                if(strcmp(buf, "exit") == 1)
                {
                    cout<<"exit";
                    send(fd2,"Exitting.",sizeof("Exitting.\n"),0);
                }else{
                cout<<buf;
                send(fd2,"Cooooolll.",sizeof("Cooooolll.\n"),0);
                }
             }
             WSACleanup();
                      
            }
    
       
       return 0;
    }
    OK thats the code, I'm just using telnet to connect to it for now.
    And as you can see I commented out the date thing because it doesn't work.

    Ok so it recieves "exit" which is put into the buffer as you can see and then it strcmp the buffer with "exit", if it is exit it sends the client (telnet in this case) a message saying Exitting and else it sends Cooolllll.

    But it doesn't work. What am I doing wrong? I'm pretty sure its something with the string compare.

    [Devouring One]
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  9. #9
    Ok, the compare still doesn't work even after I null terminated the buffer.

    Also after recieving the message from the client the server says Accept Error which is earlier on in the code. How does it jump to accepting a connection if it allready accepted the connection?

    [Devouring One]
    Last edited by devour89; 05-30-2003 at 10:29 AM.
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  10. #10
    Code:
    if((numbytes = recv(fd2,buf,MAXDATASIZE,0)) == SOCKET_ERROR)
             {
                cout<<"Recieving Error\n"<<strerror(WSAGetLastError()) ;
                return -1;
             }
             buf[numbytes] = '\0';
                if(strcmp(buf, "exit\n") == 0)
                {
                    send(fd2,"Exitting.",sizeof("Exitting."),0);
                }else{
                cout<<buf;
                send(fd2,"Cooooolll.",sizeof("Cooooolll."),0);
                }
             WSACleanup();
    Thats the code now, lol u were right bout the WSACleanup() thing it was in the loop. Ok thats fixed now! Thanx that only leaves that the compaing is still going wrong. I'm no longer using telnet just a client that I made and it uses
    Code:
    cin.getline(msg, 100, "\n")
    So it should just work shouldn't it?

    Anyways this is all in Blocking Sockets but what if I want to be recieving things while typing lets say for the sake of example having random letters going all over the console?

    Wouldn't I use
    Code:
    ioctlsocket(fd2, FIONBIO, 1);
    fd2 being the socket to change the blocking mode to off. But it tells me that I can't pass 1 as the third arguement while on msnd it says:
    The argp parameter is a pointer to an unsigned long value. Set argp to a nonzero value if the nonblocking mode should be enabled, or zero if the nonblocking mode should be disabled. When a socket is created, it operates in blocking mode by default (nonblocking mode is disabled). This is consistent with BSD sockets.
    So whats wrong with that??

    Thanx Again!
    [Devouring One]
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  11. #11
    Thanx for all the help! It works now!
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 05:16 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM