Jump to content
  • Advertisement
Sign in to follow this  
dave

Linux Socket Problem, linked to previous thread...

This topic is 4849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I posted this thread about the SNTP protocol just recently, thanks for the help with that. I have now moved on to writing my own client for connecting to that socket on my university server so i can request the time packet. This is the source code i have written to connect to the socket.
#include "../packet/packet.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT        123  // time port
#define URL         "kenny"

#define MAXDATASIZE 100

int main( int argc, char* agrv[] )
{
  struct sntpPacket myPacket;

  CompilePacket(&myPacket,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0);


  int                  sockfd;
  int                  numbytes;
  char                 buf[MAXDATASIZE];
  struct hostent*      he;
  struct sockaddr_in   their_addr;
  int                  addr_len; 

  if ((he = gethostbyname( URL )) == NULL)          {herror("gethostbyname");exit(1);};
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){perror("socket");exit(1);};

  their_addr.sin_family  = AF_INET;
  their_addr.sin_port    = htons( PORT );
  their_addr.sin_addr    = *((struct in_addr *) he->h_addr);
  memset(&(their_addr.sin_zero), '\0', 8);
  addr_len = sizeof(struct sockaddr);

  if (connect(sockfd, (struct sockaddr *) &their_addr, sizeof(struct sockaddr)) == -1)
    {
      perror("connect");
      exit(1);
    }  

  printf( "\nSending packet\n" );
  numbytes = send(sockfd, (const void*) "hello",1 , 0);
  if (numbytes == -1)
    {
      perror("send");
      exit(1);
    }
  printf("Packet Sent");

  if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) 
   { 
     perror("recv"); 
     exit(1); 
   }


  buf[numbytes] = '\0';

  printf("Recieved: %s\n", buf);
  printf("Recieved buff length = %d\n", strlen(buf));

  close(sockfd);

  return 0;
}


Now the problem that neither meor my work partner cvan figure out is that it seems to hang on the send function. I have put a string in there and sent one character from that string to simplify what needs sending, but it wasnt working when i was sending the struct for the packet. Has anyone any idea why this might not be sending. Also lastly, when i request for it to send, it will do it regardless of the server right, i mean that the client will send it out anyway. Or is it imperitive for there to be a connection set up. Baring in mind that this is a udp connection. thanks, PS: If anyone could also point me to any resources where they can suggest the values contained within a request packet, that would be cool. There is no indication in the RFC suggesting default values, or ways of finding out the information the values relate to. ace

Share this post


Link to post
Share on other sites
Advertisement
What i am eventually trying to do is construct an SNTP packet to send to an SNTP server at uni here and hopefully it should send back a similar packet with the time of the server in it. Getting somewhere with it now though.

ace

Share this post


Link to post
Share on other sites
Mind posting the code for sending the struct, not just the 1 char?

In my experience, send will only hang/block when you've tried to send too much info, and filled some sort of OS buffer. [I've only dealt with TCP though]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!