no save
Assistance
Achat
News

Forum | programmation
[langage C] Problème client/serveur
legyptien, le ven. 21 déc. 2007 à 02:08:09
Bonjour,

j ai un programme en C qui est:

un serveur echo retourne la chaine de caracteres qu il recoit. Il faut que je le modifie pour qu il retourne l adresse IP du client ainsi que son TCP.
Je sais que je dois utiliser la fonction "getpeername" et "inet_ntoa" qui utilise une structure de donnees de type sockaddr_in.

Voici les programmes echoclient et echoserveur mais il faut modifier que echoserveur(je sais pas comment j ajoute une piece jointe donc je colle mon programme en C que j ai ouvert avec "notepad" j espere qu il a pas modifier le contenu vu que c est du C):

ECHOCLIENT:
//
// 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);
}



ECHOSERVEUR:
//
// 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 beaucoup de votre aide, c est tres important! Configuration: Windows Vista
Firefox 2.0.0.11


Char Snipeur
déc. 07
Suivant
REPONSES
Char Snipeur
déc. 07
legyptien
déc. 07
legyptien
janv. 08
legyptien
janv. 08
Char Snipeur
janv. 08
legyptien
janv. 08
Char Snipeur
janv. 08
legyptien
janv. 08
djessika
oct. 08
Version Web
Réalisé par RedShift
no save