Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Problema con socket e thread
Forum - C/C++ - Problema con socket e thread

Avatar
Mattia99 (Member)
Rookie


Messaggi: 38
Iscritto: 19/02/2016

Segnala al moderatore
Postato alle 14:51
Martedì, 07/11/2017
Salve a tutti.
Ho creato una piccola chat in C con l'uso dei socket e dei thread.
Il codice del lato server funziona alla perfezione. il problema sta nel lato client.
Eseguendolo, infatti, la funzione recvfrom() mi restituisce l'errore 10022 (argomento non valido).
Non capisco davvero dove sia il problema... qualcuno è in grado di aiutarmi?

Codice sorgente - presumibilmente VB.NET

  1. void *ricevi(void *in){
  2.                 //receive a reply and print it
  3.                 //clear the buffer by filling null, it might have previously received data
  4.                 memset(buf, '\0', BUFLEN);
  5.                 //try to receive some data, this is a blocking call
  6.                 if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
  7.                 {
  8.                         printf("recvfrom() failed with error code : %d", WSAGetLastError());
  9.                         exit(EXIT_FAILURE);
  10.                 }
  11.  
  12.                 puts(buf);
  13. }


PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 15:07
Martedì, 07/11/2017
Ci mostri il main e come sono dichiarati tutti gli argomenti?


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote
Avatar
Mattia99 (Member)
Rookie


Messaggi: 38
Iscritto: 19/02/2016

Segnala al moderatore
Postato alle 15:15
Martedì, 07/11/2017
Metto di seguito tutti il listato:

Codice sorgente - presumibilmente C++

  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2.  
  3. #include<stdio.h>
  4. #include<iostream>
  5. using namespace std;
  6. #include<winsock2.h>
  7. #include <windows.h>
  8.  
  9. #pragma comment(lib,"ws2_32.lib") //Winsock Library
  10.  
  11. #define BUFLEN 512  //Max length of buffer
  12. #define PORT 2000   //The port on which to listen for incoming data
  13.  
  14.  
  15.  
  16. #include <sys/types.h>
  17. #include <pthread.h>
  18.  
  19.  
  20.         struct sockaddr_in si_other;
  21.         int s, recv_len, slen = sizeof(si_other);
  22.         char buf[BUFLEN];
  23.         char message[BUFLEN];
  24.         WSADATA wsa;
  25.        
  26.         char SERVER[15];
  27.        
  28.         pthread_t inviaT, riceviT;
  29.         int x;
  30.        
  31.         void *invia(void *);
  32.         void *ricevi(void *);
  33.        
  34. int main(void)
  35. {
  36.        
  37.  
  38.         printf("Inserisci indirizzo ip: ");
  39.         gets(SERVER);
  40.         fflush(stdin);
  41.         //Initialise winsock
  42.         //printf("\nInitialising Winsock...");
  43.         if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  44.         {
  45.                 printf("Failed. Error Code : %d", WSAGetLastError());
  46.                 exit(EXIT_FAILURE);
  47.         }
  48.         //printf("Initialised.\n");
  49.  
  50.         //create socket
  51.         if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
  52.         {
  53.                 printf("socket() failed with error code : %d", WSAGetLastError());
  54.                 exit(EXIT_FAILURE);
  55.         }
  56.  
  57.         //setup address structure
  58.         memset((char *)&si_other, 0, sizeof(si_other));
  59.         si_other.sin_family = AF_INET;
  60.         si_other.sin_port = htons(PORT);
  61.         si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
  62.  
  63.         //start communication
  64.         while (1)
  65.         {
  66.                 x = pthread_create(&inviaT, NULL, invia, NULL);
  67.                 x = pthread_create(&riceviT, NULL, ricevi, NULL);
  68.                
  69.                 pthread_join(inviaT, NULL);
  70.                 pthread_join(riceviT, NULL);
  71.         }
  72.  
  73.         closesocket(s);
  74.         WSACleanup();
  75.  
  76.         return 0;
  77. }
  78.  
  79. void *invia(void *in){
  80.         printf("Inserisci messaggio: ");
  81.         gets(message);
  82.        
  83.         if (sendto(s, message, strlen(message), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
  84.         {
  85.                 printf("sendto() failed with error code : %d", WSAGetLastError());
  86.                 exit(EXIT_FAILURE);
  87.         }
  88.  
  89. }
  90.  
  91. void *ricevi(void *in){
  92.                 //receive a reply and print it
  93.                 //clear the buffer by filling null, it might have previously received data
  94.                 memset(buf, '\0', BUFLEN);
  95.                 //try to receive some data, this is a blocking call
  96.                 if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
  97.                 {
  98.                         printf("recvfrom() failed with error code : %d", WSAGetLastError());
  99.                         exit(EXIT_FAILURE);
  100.                 }
  101.  
  102.                 puts(buf);
  103. }


PM Quote