Home > Net >  server sends back unwanted characters
server sends back unwanted characters

Time:12-24

#include <io.h>
#include <stdio.h>
#include <winsock2.h>
#include <string.h>

#define MY_PORT     8989
#define MAXBUF      256

int main(int argc, char *argv[]){
    WSADATA wsa;
    SOCKET sockfd ,clientfd; //define 2 sockets
    struct sockaddr_in self;
    char buffer[MAXBUF];

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
    printf("Initialised.\n");

    /*---create streaming socket---*/
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
        perror("Socket");
        exit(errno);
    }
    printf("Socket created.\n");

    /*---initialize address/port structure---*/
    /* bzero(&self, sizeof(self));*/
    self.sin_family = AF_INET;
    self.sin_port = htons(MY_PORT);
    self.sin_addr.s_addr = INADDR_ANY;

    /*---assign a port number to the socket---*/
    if ((bind(sockfd, (struct sockaddr*)&self, sizeof(self)))!=0){
        perror("socket--bind");
        exit(errno);
    }
    puts("Bind done");

    /*---make it a "listening socket"---*/
    if ((listen(sockfd, 20))!=0){
        perror("socket--listen");
        exit(errno);
    }
    puts("Waiting for incoming connections...");

    /*---forever... ---*/
    while (1){
        struct sockaddr_in client_addr;
        int addrlen=sizeof(client_addr);

        /*---accept a connection (creating a data pipe)---*/
        clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);

        //create variables
        int length=recv(clientfd, buffer, MAXBUF, 0);

        //output in reverse order & lowercase
        for(int i=0; buffer[i]!='\0'; i  ){
            if(buffer[i]>=65 && buffer[i]<=90){ // check ascii value
                buffer[i]= buffer[i] 32;       // if uppercase thn change
            }
        }

        int loop=0, len=strlen(buffer);
        char temp;  //temporary holds character to swap

        len--;     // remove null and start from 0

        while (loop<len){
            temp=buffer[loop];
            buffer[loop]=buffer[len];
            buffer[len]=temp;
            loop  ;
            len--;
        }

        //send back
        send(clientfd, buffer, sizeof(buffer), 0);

        /*---close connection---*/
        close(clientfd);
    }

    /*---clean up (should never get here!)---*/
    close(sockfd);
    WSACleanup();
    return 0;
}

The reversing of string from client works well but it also gives out many other unwanted characters behind it and two 'PuTTy's, I'm very new to socket programming so any help is much appreciated. I can't use any string functions other than strlen. Currentlly using puTTy to open server on 8989. Telnet connection is passive.

CodePudding user response:

You code assumes that buffer is NUL terminated, however there is no guaranty that the NUL character will be received by recv (which may be a partial recv and this is streaming socket).

It would make more sense to reverse the data using the return value from recv (if a positive value). This way you do not have to worry about message boundaries.

    int length=recv(clientfd, buffer, MAXBUF, 0);

    //output in reverse order & lowercase
    for(int i=0; i < length; i  ){
        if(buffer[i]>=65 && buffer[i]<=90){ // check ascii value
            buffer[i]= buffer[i] 32;       // if uppercase thn change
        }
    }

CodePudding user response:

You are ALWAYS sending MAXBUF (ie 256) bytes because in

send(clientfd, buffer, sizeof(buffer), 0);

sizeof(buffer) is the size of the char array declared by

#define MAXBUF      256
char buffer[MAXBUF];

Fix this by using strlen(buffer) for example, or keeping the len you got previously.

  • Related