m r
m r

Reputation: 11

sending/receiving/manipulating strings with sockets

I have a little problem with sending and receiving a string with socket between a server and a client. In this code, first the server sends "connection established ("connessione avvenuta" in Italian) to the client (which prints it on screen), then the client scans a string and sends it to the server, which deletes the vowels and sends it again to the client. When I print it with client, strange chars appears at the end of the string, I think because I didn't close it well. How to do this?

**SERVER SIDE**

#if defined WIN32
#include <winsock2.h>
#else
#define closesocket close
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 30

void ClearWinSock() {
#if defined WIN32
    WSACleanup();
#endif
}

int main(void) {

//---------------------------INIZIALIZZAZIONE WSADATA

#if defined WIN32

    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf ("Error at WSAStartup");
        return 0;
    }

#endif

//-------------------------------CREAZIONE SOCKET

  int Mysocket;
  Mysocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (Mysocket < 0) {
      printf("socket creation failed\n");
      return 0;
  }

  struct sockaddr_in sad;
  memset(&sad, 0, sizeof(sad));
  sad.sin_family = AF_INET;
  sad.sin_addr.s_addr = inet_addr ("127.0.0.1");
  sad.sin_port = htons (9888);

//------------------------ASSEGNAZIONE PORTA E IP ALLA SOCKET

if (bind(Mysocket, (struct sockaddr*) &sad, sizeof(sad)) <0) {
    printf ("bind() failed\n");
    closesocket(Mysocket);
    return 0;
}

//---------------------------SETTAGGIO SOCKET ALL'ASCOLTO

int qlen = 10;

if (listen (Mysocket, qlen) < 0) {

    printf("listen() failed\n");
    closesocket(Mysocket);
    return 0;
}

struct sockaddr_in cad;
int Csocket;
int clientlen;

//------------------------------ACCETTA LA CONNESSIONE

while (1) {

    printf("In attesa di un client con cui comunicare\n");
    memset(&cad, 0, sizeof(cad));
    clientlen = sizeof(cad);

    if((Csocket = accept(Mysocket, (struct sockaddr*) &cad, &clientlen)) < 0) {
        printf ("accept failed\n");
        closesocket(Mysocket);
        ClearWinSock();
        return 0;
        }

    printf("connesso con il client\n");


//---------------------------------------INVIO STRINGA AL CLIENT

    char* inputString = "connessione avvenuta";
    int stringlen = strlen(inputString);


    if (send(Csocket, inputString, stringlen, 0) != stringlen) {
        printf("client-send() sent a different number of bytes than expected");
        closesocket(Csocket);
        ClearWinSock();
        system ("pause");
        return 0;
    }


//-------------------------------------RICEZIONE STRINGA DAL CLIENT

    char str1[BUFSIZE+1]; char str1des[BUFSIZE+1];
    int i,j=0;

    //recv (Csocket, str1, BUFSIZE - 1, 0);

    int read = recv(Csocket, str1, BUFSIZE-1, 0); str1[read] = '\0';


//-------------------------------ELIMINAZIONE VOCALI DALLA STRINGA RICEVUTA

    for(i=0;i<=strlen(str1)+1;i++) {

        if(str1[i]=='a'||str1[i]=='e'||str1[i]=='i'||str1[i]=='o'||str1[i]=='u' ||str1[i]=='A'||str1[i]=='E'||str1[i]=='I'||str1[i]=='O'||str1[i]=='U')
                        str1[i]=' ';
                else
                        str1des[j++]=str1[i];
            }

            str1des[j]='\0';

//-----------------------------------INVIO STRINGA ELABORATA AL CLIENT

            if (send(Csocket, str1des, strlen(str1des), 0) != strlen(str1des)) {
                printf("client-send() sent a different number of bytes than expected");
                closesocket(Csocket);
                ClearWinSock();
                system ("pause");
                return 0;
            }

}

//------------------------------------------CHIUSURA CONNESSIONE

closesocket (Csocket);
ClearWinSock();
printf ("\n");
system ("pause");
return 0;

}

**CLIENT SIDE**

#if defined WIN32
#include <winsock2.h>
#else
#define closesocket close
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 30

void ClearWinSock() {
#if defined WIN32
    WSACleanup();
#endif
}

//-----------------------------INIZIALIZZAZIONE WSADATA

int main (void) {
#if defined WIN32

    WSADATA wsaData;
    int iResult = WSAStartup (MAKEWORD (2,2), &wsaData);

    if (iResult !=0) {
        printf ("error at WSASturtup\n");
        return 0;
        }

#endif


//--------------------------------CREAZIONE SOCKET

    int Csocket;

    Csocket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (Csocket < 0) {

    printf ("socket creation failed");
    closesocket (Csocket);
    ClearWinSock();
    return 0;
    }

 //--------------------------COSTRUZIONE INDIRIZZO SERVER

   struct sockaddr_in sad;
   memset (&sad, 0, sizeof(sad));
   sad.sin_family = AF_INET;
   sad.sin_addr.s_addr = inet_addr ("127.0.0.1");
   sad.sin_port = htons (9888);


//------------------------------CONNESSIONE AL SERVER

  if (connect(Csocket, (struct sockaddr*) &sad, sizeof(sad)) < 0) {
      printf ("failed to connect\n");
      closesocket (Csocket);
      ClearWinSock();
      return 0;
  }


//-----------------------------RICEZIONE DATI DAL SERVER

  char buf[BUFSIZE+1];

  int read = recv (Csocket, buf, BUFSIZE - 1, 0);

if (read <=0) {

    printf ("Qualcosa non và!\n");
}

else {
    buf[read+1] = '\0';
    printf("Server scrive: %s\n", buf);
}


//----------------------------INVIO PRIMA STRINGA AL SERVER

char str1[BUFSIZE+1]; int stringlen = strlen(str1);
printf ("inserisci prima stringa:\n");
scanf ("%s", str1);

if (send(Csocket, str1, stringlen, 0) != stringlen) {
    printf("client-send() sent a different number of bytes than expected");
    closesocket(Csocket);
    ClearWinSock();
    system ("pause");
    return 0;
}

//------------------------------RICEZIONE STRINGA ELABORATA DAL SERVER

char buf2[BUFSIZE+1];

  int read2 = recv (Csocket, buf2, BUFSIZE - 1, 0);

if (read2 <=0) {

    printf ("Qualcosa non và!\n");
}

else {
    buf2[read+1] = 0;
    printf("Server scrive: %s\n", buf2);
}

//---------------------------------------CHIUSURA CONNESSIONE

closesocket (Csocket);
ClearWinSock();
printf ("\n");
system ("pause");
return 0;

}

Upvotes: 0

Views: 160

Answers (1)

S&#233;bastien Chapuis
S&#233;bastien Chapuis

Reputation: 185

With TCP protocol, send() and recv() doesn't necessary write/read the length given in parameters. You need to check the return value to know how much bytes has been write/read on the socket.

There are 2 simple methods to send/read string on sockets:

  • Use a header.
    In the packet you write on the socket, you include the string size just before the string.
    Example, if you write "hello", the packet you'll send will be formed like this:

    +---------+---------------+
    | uint8_t  | char[size]   |
    +--------------------------+
    |    0x5    |     "hello"     |
    +---------+---------------+

    With this method, when you read on the socket, you first read the header ( 1 byte in the example ) then you read N bytes to get the complete string.
    Don't forget to add the '\0' at the end of the string.

  • Include the '/0' at the end of the string.

    +------------------+
    | char[size + 1] |
    +------------------+
    |       "hello\0"     |
    +------------------+

    By this way, you read on the socket until you get a '\0'

Upvotes: 1

Related Questions