Problem with socket

This is a discussion on Problem with socket within the Linux Programming forums, part of the Platform Specific Boards category; i got two question here: Question 1: i got the following code: while (1) { printf("\nftp> "); scanf("%s",buf); //read command ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    6

    Problem with socket

    i got two question here:

    Question 1:

    i got the following code:

    while (1) {
    printf("\nftp> ");
    scanf("%s",buf); //read command
    send(sd,buf,strlen(buf),0);

    if(strcmp(buf,"list")==0){ //if command is 'list'
    printf("Obtaining file list server...");
    n = recv(sd,buf,sizeof(buf),0); //get list of file from server
    write(1,buf,n);
    }
    }

    the output is unexpected, which is:

    ftp>list

    file1
    file2
    Obtaining file list from server...

    ftp>

    by right i should get something like:

    ftp>list

    Obtaining file list from server...
    file1
    file2

    ftp>

    isn't it? anyone help me?

    Question 2:

    I'm developing a webserver, below are part of my code...

    while(1){
    alen = sizeof(cad);
    if((sd2=accept(sd,(struct sockaddr *)&cad, &alen)) < 0){
    printf("accept failed\n");
    exit(1);
    }
    n = recv(sd2, buf, sizeof(buf), 0);

    sscanf(buf,"%s%s", request,pathname); // extract GET and filename
    printf("%s",request);
    printf("%s",pathname);

    if(!fpt=fopen(pathname,"r"){
    sprintf(buf,"<html><h1>Error 404</h1></html>
    send(1,buf,strlen(buf),0);
    }
    else{
    printf(buf,"<html><h1>Hello World</h1></html>");
    send(1,buf,strlen(buf),0);
    }
    closesocket(sd2);
    }

    the problem:

    1.
    printf("%s",request);
    printf("%s",pathname);

    this two lines never been executed... (coz no output)

    2.
    when there is no such file on the path.. the program just stop and quit... it will never gonna print out the Error 404 page...
    however, it works in the else part.. which display hello world..

    anyone?

    thanks

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    6

    Thanks

    Thanks for the help...

    i manage to solve all my problem...

    a million thanks from me!

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    6

    Another Question

    on my client side...

    Code:
    strcpy(buf,"Hello World");
    send(sd,buf,strlen(buf),0);
    on my server side...

    Code:
    n = recv(sd,buf,sizeof(buf),0);
    write(1,buf,n);
    the result i get is "Hello World^12#@$%" instead of "Hello World"

    what's wrong?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 03:46 AM
  3. Problem with network code
    By cornholio in forum Linux Programming
    Replies: 1
    Last Post: 12-20-2005, 12:21 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 09:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21