linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* more socket stuff
@ 2003-02-26 13:46 Mat Harris
  0 siblings, 0 replies; only message in thread
From: Mat Harris @ 2003-02-26 13:46 UTC (permalink / raw)
  To: linux-c-programming


[-- Attachment #1.1: Type: text/plain, Size: 1185 bytes --]

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


-- 
Mat Harris			OpenGPG Public Key ID: C37D57D9
mat.harris@genestate.com	www.genestate.com	

[-- Attachment #1.2: socket --]
[-- Type: text/plain, Size: 1029 bytes --]

#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;
}

[-- Attachment #1.3: conf.h --]
[-- Type: text/plain, Size: 1290 bytes --]

/***************************************************************************
                  conf.h  -  Stores the config for aurora
                             -------------------
    begin                : Wed Feb 26 2003
    copyright            : (C) 2003 by Mat Harris
    email                : mat.harris@genestate.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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"

[-- Attachment #1.4: aurora.c --]
[-- Type: text/plain, Size: 2468 bytes --]

/***************************************************************************
            aurora.c  -  The main code that holds aurora together
                             -------------------
    begin                : Wed Feb 26 2003
    copyright            : (C) 2003 by Mat Harris
    email                : mat.harris@genestate.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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;
}

[-- Attachment #2: Type: application/pgp-signature, Size: 232 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-02-26 13:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-26 13:46 more socket stuff Mat Harris

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).