LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Wargame n/w programming problem (https://www.linuxquestions.org/questions/programming-9/wargame-n-w-programming-problem-425098/)

live_dont_exist 03-15-2006 10:56 AM

Wargame n/w programming problem
 
Hi All,
Was playing a war game...to improve my coding n stuff...heres the problem...

Code:

Your goal is to connect to port 5842 on
vortex.labs.pulltheplug.org and read in 4 unsigned integers. Add these integers together and send back the results to get a username and password for level 1. Note that vortex is on an x86 machine (meaning, a little endian architecture).

And here's my code.....but it isnt working...anything thats obviously wrong???...I didnt try the little endian stuff...coz Im using an X86 as well...

If Im in the wrong forum..can someone point me to a placve where I can discuss C Network programming..

Thnx
Arvind

Code:


#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <stdlib.h>

int main()
{
  int sockfd=3;
  int n=0;int i=0;
  struct sockaddr_in servaddr;
  char str[14]="69.55.233.82";
  uint32_t buffer[500];
  uint32_t final=0;

  for (i=0; i<499; i++){
        buffer[i]=8888;
  }

  if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<=0){
        perror("Error creating socket");
        }
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family=AF_INET;
  servaddr.sin_port=htons(5842);

  if ((inet_pton(AF_INET,str,&servaddr.sin_addr))!=1){
        perror("Error in inet_pton");
        exit(0);
        }
 
  if (connect(sockfd,(struct sockaddr_in *)&servaddr,sizeof(servaddr))<0){
        perror("Error in connect");
        exit(0);
        }

  while ((n=read(sockfd,buffer,16)>0)){
        printf("Getting data %d\n",n);
        if (n==16){
                  printf("%d",n);
                  break;               
                }
        }

  final = buffer[0]+buffer[1]+buffer[2]+buffer[3];
  printf("FINAL %d" , final);
  n=0;
  /*while ((n=write(sockfd,&final,4)>0)){
        if (n<0){
                perror("Error in write function call");
//                exit(0);
        }
        if (n==4){
                printf("Successfully written data\n");
        }
        else{
                printf("%d\n",n);
        }
        }
  /*for (i=0; i<12; i++){
        printf("%d\n",buffer[i]);
  } */
  return 0;
}


burntfuse 03-16-2006 08:43 PM

I'm not an expert at socket programming (only done it a few times), but you could try a packet sniffer like Ethereal to see what's actually being received from the server and transmitted from your program.

BTW, what site is that from (the wargame, that is)?

xhi 03-16-2006 10:19 PM

> BTW, what site is that from (the wargame, that is)?
http://pulltheplug.org

live_dont_exist 03-16-2006 11:14 PM

Hi ,
Got a bit of help from the guys at (Yeah..pulltheplug) ..the forum...seems Id put in one loop too many(bad oversight)...n the code ws crashing....is fixed now...just post here ...incase U'd like 2 c the code...n how twas fixed...

Thnx
On to L1...
p.s...Burntfuse: ethereal wouldnt hav been helpful here..as I ws printing out the recv buffer n cud see it ws null...


All times are GMT -5. The time now is 09:28 AM.