Bonjour,

je test les sockets en C sous linux et j'ai realisé un code serveur un code client . Le client se connecte et envoie une chaine de caractere qui est retournee en majuscule par le serveur.

Tous se passe bien en UDP mais en TCP mon code serveur plante a la fonction recv qui me renvoie -1. Ci dessous mon code serveur si qqun a une idee merci d'avance!!!

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 
#include     <arpa/inet.h>
#include     <netdb.h>
#include     <netinet/in.h>
#include     <iostream>
#define MAX_MSG 65535
using namespace std;
int main()
{
   int listenSocket, i;
   unsigned short int listenPort;
   socklen_t clientAddressLength;
   struct sockaddr_in clientAddress, serverAddress;
   char line[(MAX_MSG+1)];
   cout << "Enter port number to listen on (between 1500 and 65000): ";
   cin >> listenPort;
   // Create socket for listening for client connection requests.
	listenSocket = socket(AF_INET, SOCK_STREAM, 0);//TCP : SOCK_STREAM, UDP : SOCK_DGRAM
   if (listenSocket < 0) {
 
      cerr << "cannot create listen socket";
      exit(1);
   }
   // Bind listen socket to listen port. First set various fields in
   // the serverAddress structure, then call bind().
   // htonl() and htons() convert long integers and short integers
   // (respectively) from host byte order (on x86 this is Least
   // Significant Byte first) to network byte order (Most Significant
   // Byte first).
   serverAddress.sin_family = AF_INET;
   serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
   serverAddress.sin_port = htons(listenPort);
   if (bind(listenSocket,
                (struct sockaddr *) &serverAddress,
                sizeof(serverAddress)) < 0) {
      cerr << "cannot bind socket";
      exit(1);
   }
   // Wait for connections from clients.
   // This is a non-blocking call; i.e., it registers this program with
   // the system as expecting connections on this socket, and then
   // this thread of execution continues on.
   listen(listenSocket, 5);
   cout << "Waiting for request on port " << listenPort << "\n";
   while (1) {
      clientAddressLength = sizeof(clientAddress);
      // First set line to all zeroes, so we'll know where
    // the end of the string is.
    memset(line, 0x0, (MAX_MSG+1));
 
	 //ci dessous le cas UDP
	/*int status = recvfrom(listenSocket, line, MAX_MSG, 0,
                 (struct sockaddr *) &clientAddress,             UDP
                 &clientAddressLength);*/
 
	 int status = accept(listenSocket,(struct sockaddr *) &clientAddress,&clientAddressLength);
 
	 if (status < 0)
	 {
		 cerr << "erreur accept " << status << endl;
		 exit(1);
	 }
 
 
	status = recv(listenSocket, line, MAX_MSG, 0);	
	// status = read(listenSocket,line,1);
	if (status < 0)
	{
		cerr << " I/O Problem : " << status << endl /*<< WSAGetLastError() << endl*/;
      exit(1);
    }
    // show the client's IP address
    cout << " from " << inet_ntoa(clientAddress.sin_addr);
    // Show the client's port number.
    cout << ":" << ntohs(clientAddress.sin_port) << "\n";
    // Show the line
    cout << " Received: " << line << "\n";
    // Convert line to upper case.
    for (i = 0; line[i] != '\0'; i++)
      line[i] = toupper(line[i]);
    // Send converted line back to client.
    if (sendto(listenSocket, line, strlen(line) + 1, 0,
               (struct sockaddr *) &clientAddress,
               sizeof(clientAddress)) < 0)
      cerr << "Error: cannot send modified data";
    memset(line, 0x0, (MAX_MSG+1));       // set line to all zeroes
  }
}