Advertisement

Linux Socket Problem, linked to previous thread...

Started by August 05, 2005 08:40 AM
2 comments, last by Telastyn 19 years, 6 months ago
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
you are trying to send and recieve a text? between 2 users?
This world is ruled by big furry CATS!! :)Meow! :)
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
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]

This topic is closed to new replies.

Advertisement