Pb d'execution d'un clien_serv sous Dev C++

Fermé
legyptien Messages postés 382 Date d'inscription lundi 19 novembre 2007 Statut Membre Dernière intervention 23 avril 2022 - 27 déc. 2007 à 16:32
legyptien Messages postés 382 Date d'inscription lundi 19 novembre 2007 Statut Membre Dernière intervention 23 avril 2022 - 27 déc. 2007 à 16:50
Bonjour,

J'ai un programme appelé echoclient et un autre appeler echoserveur. Ils sont justes car c'est le professeur qui les a fait et les a compilé (sous linux) . Le probleme c est que je suis sous windows , j ai telecharger et installer Dev C++ et je sais pas comment lancer ces executables.
(je sais qu il faut taper "$./echoServer 900 " pour lancer le serveur et $./echoClient <port> <adresse_du_serveur> mais ca c est sous linux et en plus je sais pas ce qu il faut mettre a la place de <adresse_du_serveur>).
mes 2 programmes "echoclients" et "echoserveur" realise la chose suivante, le client envoie une chaine de caractere et le serveur lui renvoie (comme un ecco donc).Pour info voici les programmes:

VOICI ECCHOCLIENT
//
// Socket client using TCP
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stream.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

#define MAXLEN 256

// ---------------------------------------------------------------------
// This function:
// - reads a string from std input (until '\n')
// - writes it to the socket
// - receives the copy through the socket
// - writes it back to the std output
// ---------------------------------------------------------------------
void str_cli(int sockfd)
{
char ws[MAXLEN];
char rs[MAXLEN];
int n_bytes, n_written, n_read;

// read the string to send
cout << "Please enter a string (no spaces)" << endl;
cin >> ws;
n_bytes = strlen(ws)+1;

// write on socket
n_written = write(sockfd, ws, n_bytes);

// see if you missed some characters
if (n_written < n_bytes)
fprintf(stderr, "Client warning: incomplete string sent\n");

// read the answer from the socket
n_read = read(sockfd, rs, MAXLEN);

// see if you lost some more characters on the way back
if (n_read < n_written)
fprintf(stderr, "Client warning: incomplete string received\n");

// writes the received string
cout << "This is the reply that I got back from the other side:" << endl;
cout << rs << endl;
}



// ---------------------------------------------------------------------
// main
// ---------------------------------------------------------------------
main (int argc, char* argv[])
{

int sockfd, clilen;
struct sockaddr_in serv_addr;

// check command line
if (argc < 3) {
cout << "Usage: echoClient <server port number> <server hostname>" << endl;
return(0);
}

// port number is given as first argument
const int serv_port = atoi(argv[1]);

// retrieve host information from name
struct hostent* hptr = gethostbyname(argv[2]);
if (hptr == NULL) {
cout << "echoClient: Error in gethostbyname(), "
<< "please check server host name" << endl;
return(0);
}

// look in address list
char** addrlist = hptr->h_addr_list;
struct in_addr* aptr;

aptr = (struct in_addr*) addrlist[0];
char* serv_ip = inet_ntoa(*aptr);

// Fill in the structure with the server address
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
serv_addr.sin_port = htons(serv_port);

// Open a TCP socket
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "Client: error, cannot open stream socket\n");
return(0);
}


// Connect to the server
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
fprintf(stderr, "Client: error, cannot connect to server\n");
return(0);
}


// send the request and exit
str_cli(sockfd);

close(sockfd);
return(0);
}

VOICI ECCHOSERVER

//
// Socket server using TCP
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stream.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

// #define SERV_TCP_PORT 6321
// #define SERV_HOST_ADDR "193.55.114.167" /* faron.eurecom.fr */

#define MAXLEN 255

// This function:
// - reads a string from a socket
// - writes it back to the same socket
void str_echo(int sockfd)
{
char s[MAXLEN];
int n_written, n_read;

// read the string from socket
n_read = read(sockfd, s, MAXLEN);

// print the received string
cout << "String read: " << s << endl;

// write back
n_written = write(sockfd, s, n_read);

// see if you missed some characters
if (n_written < n_read)
fprintf(stderr, "Server warning: incomplete string sent\n");
}



main (int argc, char* argv[])
{

int sockfd, newsockfd;
socklen_t clilen;
struct sockaddr_in cli_addr, serv_addr;

// server port number
if (argc < 2) {
cout << "Argument: server port number" << endl;
return(0);
}
int serv_port = atoi(argv[1]);

// Open a TCP socket
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "Error in server: can't open socket\n");
return(0);
}

// bind the local address with specified port number
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(serv_port);

if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
fprintf(stderr, "Error in server: can't bind local address\n");
return(0);
}

listen(sockfd,1);

for (;;) {

// Wait for connections from clients.
// Iterative server.
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

if (newsockfd < 0)
{
fprintf(stderr, "Server: accept error.\n");
return(0);
}

// Process the request.
cout << "Request received, ..." << endl;
str_echo(newsockfd);

// close the socket while waiting
close(newsockfd);
}

// close listening socket
close(sockfd);
}


merci d avance.
A voir également:

2 réponses

legyptien Messages postés 382 Date d'inscription lundi 19 novembre 2007 Statut Membre Dernière intervention 23 avril 2022 9
27 déc. 2007 à 16:39
EN FAIT IL ME DIT QUE MON PROJET EST VIDE POURTANT J AI CREE UN PROJET ET J AI SOUVEGARDER!! C EST DE LA QUE VIENT LE PROBLEME .....
0
legyptien Messages postés 382 Date d'inscription lundi 19 novembre 2007 Statut Membre Dernière intervention 23 avril 2022 9
27 déc. 2007 à 16:50
c est bon c est surement qu il tourne pas sous mon vista : http://www.dil.univ-mrs.fr/~garreta/generique/autres/UtiliserDevCpp.html

c est regler.
0