Skip to contentSkip to conversation

more socket stuff

DISCUSSION Mat Harris 1 participant 1 message Feb 26, 2003

lore
Share & tools
lore

Mat Harris

author
#0 lore raw

i was just playing around with the sockets in an attempt to better understand them when i found this.

i have a very simple client and server. the server is written in perl and was just there to print whatever the client sent it, and i works fine. However, when i send it the same data from the C app, i just messes me round. Below is the output from the two commands.

telnet:
NICK aurora aurora aurora aurora aurora
USER aurora

C app:
USER aurora aurora aurora aurora aurora À4@?@aé@?@7å@9@1@À4@?ôÿ¿Ç@ÑÅ@Ð6@`óÿ¿ÐBè1@JBè1@rocr/ÐBx4@¸@è1@
yé@À4G@JBÐB`óÿ¿¨²Bè1@àòÿ¿óÿ¿À4@è1@8ôÿ¿]Ë@X @Hôÿ¿7@À4@¸@pôÿ¿Põÿ¿À@À@µl@@@¸]øõÿ¿l@½@À4@øõÿ¿@^ÑÂdø 0@Ð6@ÂUÉÂdøÿÿÿÿñ@À4@(øÿ¿ÛÚ@<öÿ¿%Ø@Âöÿ¿ôôôLinuxmaiden.genestate.com2.4.18-3smpNICK aurora
urora aurora aurora aurora À4@?@aé@?@7å@9@1@À4@?ôÿ¿Ç@ÑÅ@Ð6@`óÿ¿ÐBè1@JBè1@rocr/ÐBx4@¸@è1@
yé@À4G@JBÐB`óÿ¿¨²Bè1@àòÿ¿óÿ¿À4@è1@8ôÿ¿]Ë@X @Hôÿ¿7@À4@¸@pôÿ¿Põÿ¿À@À@µl@@@¸]øõÿ¿l@½@À4@øõÿ¿@^ÑÂdø 0@Ð6@ÂUÉÂdøÿÿÿÿñ@À4@(øÿ¿ÛÚ@<öÿ¿%Ø@Âöÿ¿ôôôLinuxmaiden.genestate.com2.4.18-3smp

hm? is all that my fault. i have attached the client

Signature · 2 lines
Mat Harris			OpenGPG Public Key ID: C37D57D9
[email protected]	www.genestate.com	

#define MAXDATASIZE 100 /* Max number of bytes of data */

int start_socket()
{
int fd, numbytes; /* files descriptors */
char buf[MAXDATASIZE]; /* buf will store received text */

struct hostent *he; /* structure that will get information about remote host */
struct sockaddr_in server; /* server's address information */

  if ((he=gethostbyname(SERVER))==NULL){       /* calls gethostbyname() */
    printf("gethostbyname() error\n");
    exit(-1);
  }

  if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){  /* calls socket() */
    printf("socket() error\n");
    exit(-1);
  }

  server.sin_family = AF_INET;
  server.sin_port = htons(PORT); /* htons() is needed again */
  server.sin_addr = *((struct in_addr *)he->h_addr);  /*he->h_addr passes "*he"'s info to "h_addr" */
  bzero(&(server.sin_zero),8);

  if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){ /* calls connect() */
    perror("connect() error");
    exit(-1);
  }

return fd;
}

/***************************************************************************
conf.h - Stores the config for aurora
-------------------
begin : Wed Feb 26 2003
copyright : (C) 2003 by Mat Harris
email : [email protected]
***************************************************************************/

/***************************************************************************

 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *

***************************************************************************/

/* The server that aurora will connect to */
#define SERVER "127.0.0.1"

/* The port to connect to */
#define PORT 8850

/* The name to use when connecting -- PLEASE CHANGE THIS */ #define BOTNICK "aurora"

/* Which channel to connect to */
#define CHANNEL "#cpg"

/***************************************************************************
aurora.c - The main code that holds aurora together
-------------------
begin : Wed Feb 26 2003
copyright : (C) 2003 by Mat Harris
email : [email protected]
***************************************************************************/

/***************************************************************************

 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *

***************************************************************************/

/* Load up all the necessary system libraries */
#include <stdio.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

/* Load all my libraries and headers for this project */ #include "conf.h"
#include "socket"

int main()
{
int fdnet, count;
char readbuf[1024], wbuf[1024], scratch[1024];

  if ((fdnet = start_socket(PORT)) < 0)
  {
    perror("Socket Error");
    exit(1);
  }
  else
  {
    printf ("Socket connected\n");
    strcpy(wbuf, "USER ");
    strcat(wbuf, BOTNICK);
    strcat(wbuf, " ");
    strcat(wbuf, BOTNICK);
    strcat(wbuf, " ");
    strcat(wbuf, BOTNICK);
    strcat(wbuf, " ");
    strcat(wbuf, BOTNICK);
    strcat(wbuf, " ");
    strcat(wbuf, BOTNICK);
    strcat(wbuf, "\n");
    write(fdnet, wbuf, sizeof(wbuf));
    printf("Sent: %s\n", wbuf);
    strcpy(wbuf, "NICK ");
    strcat(wbuf, BOTNICK);
    strcat(wbuf, "\n");
    write(fdnet, wbuf, sizeof(wbuf));
    printf("Sent: %s", wbuf);

    read(fdnet, readbuf, sizeof(readbuf));
    printf("%s", readbuf);
/*    strcpy(scratch, readbuf[0]);
    strcat(scratch, readbuf[1]);
    strcat(scratch, readbuf[2]);
    strcat(scratch, readbuf[3]);
    pingreply(readbuf);

    strcpy(wbuf, "JOIN ");
    strcat(wbuf, CHANNEL);
    strcat(wbuf, "\n\r");
    write(fdnet, wbuf, sizeof(wbuf));*/

}

return 1;
}