Client / Serveur EN C
Résolu
LaZo61
Messages postés
53
Date d'inscription
Statut
Membre
Dernière intervention
-
LaZo61 Messages postés 53 Date d'inscription Statut Membre Dernière intervention -
LaZo61 Messages postés 53 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
Voici mon code :
//Serveur//
/**********************************************************************************************
MAIN
***********************************************************************************************/
int
main (int argc, char *argv[])
{
struct sockaddr_in fsin;
int msock; /* listening socket fd */
fd_set rfds;
fd_set afds;
int alen;
int fd;
int option;
char buf[BUFSIZE];
msock = passiveTCP (service, QLEN);
/* still wrong, but at least do not coredump */
nfds = min (getdtablesize (), FD_SETSIZE);
/*Initialisation of memory adress*/
FD_ZERO (&afds);
FD_SET (msock, &afds);
char hn[256];
gethostname (hn, sizeof (hn));
while (1)
{ memcpy (&rfds, &afds, sizeof (rfds));
printf("waiting for data from Commbox...\n");
if (select (nfds, &rfds, NULL, NULL, NULL) < 0)
{
printf("Error of connection with Commbox\n");
if (errno == EINTR)
continue;
errexit ("select");
}
if (FD_ISSET (msock, &rfds))
{
int ssock;
alen = sizeof (fsin);
ssock = accept (msock, (struct sockaddr *) &fsin, &alen);
printf("Data is recevied succesful by the Commbox\n");
if (ssock < 0)
errexit ("accept");
FD_SET (ssock, &afds);
}
}
}
//CLIENT
/**********************************************************************************************
MAIN
***********************************************************************************************/
send_toserver()
{
int length;
char buf[BUFSIZE];
/*send the packet to the server*/
sprintf (buf, "Coucou baby"
);
write (dsock, buf, strlen (buf));
}
int main (int argc, char *argv[])
{ char *serverport ="1026";
char *addserver ="localhost" ;
struct sockaddr_in fsin;
fd_set rfds;
fd_set afds;
int alen;
int fd;
int option;
int input; /* GPS device fd */
char buf[BUFSIZE];
int sentdgps = 0, fixcnt = 0;
if (dsock < 0)
{
fprintf (stderr, "Can't connect to the server\n");
}
/* still wrong, but at least do not coredump */
nfds = min (getdtablesize (), FD_SETSIZE);
/*Initialisation of memory adress*/
FD_ZERO (&afds);
char hn[256];
gethostname (hn, sizeof (hn));
while (1){
/*Connection to the server and sending Data*/
dsock = connectsock (addserver,serverport, "tcp");
printf("c bon\n");
send_toserver();
FD_SET(dsock,&afds);
printf("Data Sending to the deamon\n");
}
}
//Un fichier netlib.c
#include "config.h"
#include <stdlib.h>
#include <string.h>
#if defined (HAVE_STRINGS_H)
#include <strings.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#if defined (HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif
#include "gpsd.h"
#if !defined (INADDR_NONE)
#define INADDR_NONE ((in_addr_t)-1)
#endif
extern int errexit (char *s);
static char mbuf[128];
int passivesock(char *service, char *protocol, int qlen)
{
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s, type, i;
bzero((char *) &sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if ( (pse = getservbyname(service, protocol)) )
sin.sin_port = htons(ntohs((u_short) pse->s_port));
else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
sprintf(mbuf, "Can't get \"%s\" service entry.\n", service);
errexit(mbuf);
}
if ((ppe = getprotobyname(protocol)) == 0) {
sprintf(mbuf, "Can't get \"%s\" protocol entry.\n", protocol);
errexit(mbuf);
}
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
errexit("Can't create socket:");
i = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0) {
sprintf(mbuf, "Can't set SO_REUSEADDR, port %s", service);
errexit(mbuf);
}
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
sprintf(mbuf, "Can't bind to port %s", service);
errexit(mbuf);
}
if (type == SOCK_STREAM && listen(s, qlen) < 0) {
sprintf(mbuf, "Can't listen on %s port:", service);
errexit(mbuf);
}
return s;
}
int passiveTCP(char *service, int qlen)
{
return passivesock(service, "tcp", qlen);
}
int connectsock(char *host, char *service, char *protocol)
{
struct hostent *phe;
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s, type, error,errno;
bzero((char *) &sin, sizeof(sin));
sin.sin_family = AF_INET;
if ( (pse = getservbyname(service, protocol)) )
sin.sin_port = htons(ntohs((u_short) pse->s_port));
else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
printf("Can't get \"%s\" service entry.\n", service);
}
if ( (phe = gethostbyname(host)) )
bcopy(phe->h_addr, (char *) &sin.sin_addr, phe->h_length);
else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
printf("Can't get host entry: \"%s\".\n", host);
}
if ((ppe = getprotobyname(protocol)) == 0) {
printf("Can't get \"%s\" protocol entry.\n", protocol);
}
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
errexit("Can't create socket:");
printf("ok\n");
if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0)
{ printf("c ici\n");
printf("Can't connect to %s on the port %s \n", host, service);
}
return s;
}
int connectTCP(char *host, char *service)
{
return connectsock(host, service, "tcp");
}
Mon probleme est le suivant j'arrive a etablir la connection entre le client et le serveur mais la connection TCP ne se fait pas correctement :
Nous avons Un ASK,SYN+ASK,SYN apres le client lui envoie un PSH alors que je n'arrive pas a voir pourquoi?
qulqu'un aura une idee?
Merci d'avance
Voici mon code :
//Serveur//
/**********************************************************************************************
MAIN
***********************************************************************************************/
int
main (int argc, char *argv[])
{
struct sockaddr_in fsin;
int msock; /* listening socket fd */
fd_set rfds;
fd_set afds;
int alen;
int fd;
int option;
char buf[BUFSIZE];
msock = passiveTCP (service, QLEN);
/* still wrong, but at least do not coredump */
nfds = min (getdtablesize (), FD_SETSIZE);
/*Initialisation of memory adress*/
FD_ZERO (&afds);
FD_SET (msock, &afds);
char hn[256];
gethostname (hn, sizeof (hn));
while (1)
{ memcpy (&rfds, &afds, sizeof (rfds));
printf("waiting for data from Commbox...\n");
if (select (nfds, &rfds, NULL, NULL, NULL) < 0)
{
printf("Error of connection with Commbox\n");
if (errno == EINTR)
continue;
errexit ("select");
}
if (FD_ISSET (msock, &rfds))
{
int ssock;
alen = sizeof (fsin);
ssock = accept (msock, (struct sockaddr *) &fsin, &alen);
printf("Data is recevied succesful by the Commbox\n");
if (ssock < 0)
errexit ("accept");
FD_SET (ssock, &afds);
}
}
}
//CLIENT
/**********************************************************************************************
MAIN
***********************************************************************************************/
send_toserver()
{
int length;
char buf[BUFSIZE];
/*send the packet to the server*/
sprintf (buf, "Coucou baby"
);
write (dsock, buf, strlen (buf));
}
int main (int argc, char *argv[])
{ char *serverport ="1026";
char *addserver ="localhost" ;
struct sockaddr_in fsin;
fd_set rfds;
fd_set afds;
int alen;
int fd;
int option;
int input; /* GPS device fd */
char buf[BUFSIZE];
int sentdgps = 0, fixcnt = 0;
if (dsock < 0)
{
fprintf (stderr, "Can't connect to the server\n");
}
/* still wrong, but at least do not coredump */
nfds = min (getdtablesize (), FD_SETSIZE);
/*Initialisation of memory adress*/
FD_ZERO (&afds);
char hn[256];
gethostname (hn, sizeof (hn));
while (1){
/*Connection to the server and sending Data*/
dsock = connectsock (addserver,serverport, "tcp");
printf("c bon\n");
send_toserver();
FD_SET(dsock,&afds);
printf("Data Sending to the deamon\n");
}
}
//Un fichier netlib.c
#include "config.h"
#include <stdlib.h>
#include <string.h>
#if defined (HAVE_STRINGS_H)
#include <strings.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#if defined (HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif
#include "gpsd.h"
#if !defined (INADDR_NONE)
#define INADDR_NONE ((in_addr_t)-1)
#endif
extern int errexit (char *s);
static char mbuf[128];
int passivesock(char *service, char *protocol, int qlen)
{
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s, type, i;
bzero((char *) &sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if ( (pse = getservbyname(service, protocol)) )
sin.sin_port = htons(ntohs((u_short) pse->s_port));
else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
sprintf(mbuf, "Can't get \"%s\" service entry.\n", service);
errexit(mbuf);
}
if ((ppe = getprotobyname(protocol)) == 0) {
sprintf(mbuf, "Can't get \"%s\" protocol entry.\n", protocol);
errexit(mbuf);
}
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
errexit("Can't create socket:");
i = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0) {
sprintf(mbuf, "Can't set SO_REUSEADDR, port %s", service);
errexit(mbuf);
}
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
sprintf(mbuf, "Can't bind to port %s", service);
errexit(mbuf);
}
if (type == SOCK_STREAM && listen(s, qlen) < 0) {
sprintf(mbuf, "Can't listen on %s port:", service);
errexit(mbuf);
}
return s;
}
int passiveTCP(char *service, int qlen)
{
return passivesock(service, "tcp", qlen);
}
int connectsock(char *host, char *service, char *protocol)
{
struct hostent *phe;
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s, type, error,errno;
bzero((char *) &sin, sizeof(sin));
sin.sin_family = AF_INET;
if ( (pse = getservbyname(service, protocol)) )
sin.sin_port = htons(ntohs((u_short) pse->s_port));
else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
printf("Can't get \"%s\" service entry.\n", service);
}
if ( (phe = gethostbyname(host)) )
bcopy(phe->h_addr, (char *) &sin.sin_addr, phe->h_length);
else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
printf("Can't get host entry: \"%s\".\n", host);
}
if ((ppe = getprotobyname(protocol)) == 0) {
printf("Can't get \"%s\" protocol entry.\n", protocol);
}
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
errexit("Can't create socket:");
printf("ok\n");
if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0)
{ printf("c ici\n");
printf("Can't connect to %s on the port %s \n", host, service);
}
return s;
}
int connectTCP(char *host, char *service)
{
return connectsock(host, service, "tcp");
}
Mon probleme est le suivant j'arrive a etablir la connection entre le client et le serveur mais la connection TCP ne se fait pas correctement :
Nous avons Un ASK,SYN+ASK,SYN apres le client lui envoie un PSH alors que je n'arrive pas a voir pourquoi?
qulqu'un aura une idee?
Merci d'avance
A voir également:
- Client / Serveur EN C
- Cybera client - Télécharger - Divers Réseau & Wi-Fi
- Changer serveur dns - Guide
- Filezilla client - Télécharger - Téléchargement & Transfert
- Filezilla client ou serveur ✓ - Forum Réseau
- Formate pour taxer client frigo vide ✓ - Forum Matériel & Système
8 réponses
J'ai un code pour win :
#include <string> #include <reseau.hpp> #include <iostream> #include <stdlib.h> #include <liste.hpp> #include <maj.hpp> std::string g,l; char *c=new char; int main(int argc,char **argv) { if (argc<4)exit(0); g=lliste(); std::cout<<g<<"chaine\n"; l=argv[2]; l+=" "; l+=argv[3]; #ifdef _WIN32 WSADATA WSAData; WSAStartup(MAKEWORD(2,0),&WSAData); #endif SOCKET C,S; SOCKADDR_IN Ci,Si; // definition du socket du client /* C=socket(AF_INET,SOCK_STREAM,0); Ci.sin_addr.s_addr=htonl(INADDR_ANY); Ci.sin_family=AF_INET; Ci.sin_port=htons(333); bind(C,(SOCKADDR *)&Ci,sizeof(SOCKADDR)); */ // definition du socket du serveur S=socket(AF_INET,SOCK_STREAM,0); std::cout<<S; Si.sin_addr.s_addr=inet_addr(argv[1]); Si.sin_family=AF_INET; Si.sin_port=htons(333); //system("PAUSE"); std::cout<<bind(S,(SOCKADDR *)&Si,sizeof(Si)); //system("PAUSE"); std::cout<<connect(S,(SOCKADDR *)&Si,sizeof(Si)); //system("PAUSE"); // autentification send(S,l.c_str(),sizeof(l.c_str()),0); recv(S,c,sizeof(c),0); std::cout<<c; // corps maj(S,Si,g);//envoie de la mise à jour de la liste //closesocket(C); closesocket(S); WSACleanup(); return 0; }reseu.hpp:
#ifdef _WIN32 #include <winsock2.h> #else #include <sys/socket> #endifMais il devrai fonctionné sous Linux moyennant quelques petites modif.
8-| Il en manque des bout non ?
Je comprend pas ce que tu veux faire.
Tu es sur du write(dsock...) ? moi en socket j'utilise send et recv
Je comprend pas ce que tu veux faire.
Tu es sur du write(dsock...) ? moi en socket j'utilise send et recv
Je suis sure parceque si j'utilise pas mon serveur a moi et que j'utilise un serveur http ca fonctionne avec dsock le truc c que je voies bien que le message est envoyer avec ethereal mais le probleme c'est qu'il lenvoie en psh :(
?? Je comprend pas ce que tu dit.PSH quoi kes ??
Pour moi client et serveur sont 2 prog différents.
Or là si tu regarde client, dsock n'est pas déclarer je comprend même pas que ça compile.
Un conseil pour la suite sur le forum : utilise les balises code pour mettre les sources ! ça conserve les indentation, ce qui n'est pas du luxe lorsque tu met autant de lignes.
Pour moi client et serveur sont 2 prog différents.
Or là si tu regarde client, dsock n'est pas déclarer je comprend même pas que ça compile.
Un conseil pour la suite sur le forum : utilise les balises code pour mettre les sources ! ça conserve les indentation, ce qui n'est pas du luxe lorsque tu met autant de lignes.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
g pas mie tou le prog
dsock=connectsock(host,port,"tcp");
En faite quand je le lance le client et le serveur
Je voie bien qu'une connection s'etablie entre le client et le serveur de type TCP
mais le client envoie ensuite une trame PSH c'est mon probleme
dsock=connectsock(host,port,"tcp");
En faite quand je le lance le client et le serveur
Je voie bien qu'une connection s'etablie entre le client et le serveur de type TCP
mais le client envoie ensuite une trame PSH c'est mon probleme
connait pas PSH.
à mon avis ça viens du write(). dans send_toserver() De toute façon il n'y a qu'a cet endroit que quelque chose est envoyé sur le réseau.
Je te conseil de changer write par send.
i il manque un argument, met 0.
à mon avis ça viens du write(). dans send_toserver() De toute façon il n'y a qu'a cet endroit que quelque chose est envoyé sur le réseau.
Je te conseil de changer write par send.
i il manque un argument, met 0.