[C] Programme Serveur/Client - Creepy_p0ney - 10-10-2012
Salut,j'ai donc réusi à programmer mon serveur multi clients, mais le problème se pose pour le client car en effet, mon clients fonctionne quand je me connecte au serveur (qui est sur mon linux) avec le client(qui est sur mon linux) le truc c'est que quand je me connecte avec mon client sous win ça ne fonctionne pas!! (il envoie les données mais ne les reçoit pas).
voici le code de mon srv Code : #include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
typedef struct
{
int sock;
char name[64];
}Client;
int main(){
int reals;
int sin_size;
int sockfd;
struct sockaddr_in their_addr[5];
int i=0;
int ret=0;
char buf[64];
char msg[64];
int k=0;
int m=0;
int max=0;
Client clients[5];
fd_set fd;
struct sockaddr *addr;
char *log="tim";
struct sockaddr_in my_addr;
pid_t pid;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
perror("socket");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(2300);
my_addr.sin_addr.s_addr=INADDR_ANY;
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr_in))<0){
perror("bind");
exit(1);
}
listen(sockfd,5);
printf("attente d'un client");
sin_size = sizeof(struct sockaddr_in);
printf("%d\n",i);
while(1){
int l=0;
int n=0;
FD_ZERO(&fd);
FD_SET(sockfd,&fd);
for(l=0;l<i;l++){
FD_SET(clients[l].sock,&fd);
}
printf("nous y sommes3\n");
for(n=0;n<i;n++){
if(max<clients[n].sock){
max=clients[n].sock;
}
}
if(max<sockfd)
max=sockfd;
printf("%d",max);
if((ret=select(max+1,&fd,NULL,NULL,NULL))==-1){
perror("select");
exit(1);
}
printf("nous y sommes2\n");
if(FD_ISSET(sockfd,&fd)){
printf("here we are\n");
sin_size=sizeof(struct sockaddr_in);
clients[i].sock=accept(sockfd,(struct sockaddr *)&their_addr[i],&sin_size);
send(clients[i].sock,"login :",64,0);
reals=recv(clients[i].sock,buf,64,0);
buf[reals]='\0';
strncpy(clients[i].name, buf, 64 - 1);
i++;
}
else if(ret==0){
printf("problem?");
}
else{
printf("here we are9");
for(k=0;k<i;k++){
if(FD_ISSET(clients[k].sock,&fd)){
int j=0;
printf("here we are5\n");
reals=recv(clients[k].sock,buf,64,0);
printf("here we are6\n");
if(reals==0){
close(clients[k].sock);
i--;
}
else{
buf[reals]='\0';
printf("%s",buf);
printf("here we are7\n");
strncpy(msg,clients[k].name,64-1);
strncat(msg,buf,64-strlen(msg)-1);
printf("here we are8\n");
for(j=0;j<i;j++){
send(clients[j].sock,msg,64,0);
}
}
}
}
}
}
}
celui de mon client win :Code : #if defined (WIN32)
#include <winsock2.h>
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(){
#if defined (WIN32)
WSADATA WSAData;
int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
#else
int erreur = 0;
#endif
struct sockaddr_in my_addr;
int sockfd;
char buffer[64];
char *buf;
int reals=0;
fd_set fd;
sockfd=socket(AF_INET,SOCK_STREAM,0);
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(2300);
my_addr.sin_addr.s_addr=inet_addr("192.168.1.12");
connect(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr));
while(1){
FD_ZERO(&fd);
FD_SET(STDIN_FILENO, &fd);
FD_SET(sockfd,&fd);
select(sockfd+1,&fd,NULL,NULL,NULL);
if(FD_ISSET(STDIN_FILENO, &fd))
{
fgets(buffer, 64 - 1, stdin);
{
char *p = NULL;
p = strstr(buffer, "\n");
if(p != NULL)
{
*p = 0;
}
else
{
/* fclean */
buffer[64- 1] = 0;
}
}
send(sockfd,buffer,64,0);
}
else {
reals=recv(sockfd,buf,64,0);
if(reals==0){
printf("mother fucking problem!!!");
}
puts(buf);
}
}
#if defined (WIN32)
WSACleanup();
#endif
return EXIT_SUCCESS;
}
RE: an other thing about sockets - Creepy_p0ney - 13-10-2012
Ok donc après une bonne aide de super snail j'en suis ici : Code : #include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
typedef struct
{
int sock;
char name[64];
}Client;
int main(int argc, char** argv){
int reals;
int sin_size;
int sockfd;
struct sockaddr_in their_addr[5];
int i=0;
int ret=0;
char buf[64];
char msg[64];
int k=0;
int m=0;
int max=0;
Client clients[5];
fd_set fd;
struct sockaddr *addr;
char *log="tim";
struct sockaddr_in my_addr;
pid_t pid;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
perror("socket");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(atoi(argv[1]));
my_addr.sin_addr.s_addr=INADDR_ANY;
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr_in))<0){
perror("bind");
exit(1);
}
listen(sockfd,5);
printf("attente d'un client");
sin_size = sizeof(struct sockaddr_in);
printf("%d\n",i);
while(1){
int l=0;
int n=0;
FD_ZERO(&fd);
FD_SET(sockfd,&fd);
for(l=0;l<i;l++){
FD_SET(clients[l].sock,&fd);
}
printf("nous y sommes3\n");
for(n=0;n<i;n++){
if(max<clients[n].sock){
max=clients[n].sock;
}
}
if(max<sockfd)
max=sockfd;
printf("Socket %d\n",max);
if((ret=select(max+1,&fd,NULL,NULL,NULL))==-1){
perror("select");
exit(1);
}
// On regarde si le dernier socket a des données
if(FD_ISSET(sockfd,&fd)){
printf("here we are\n");
sin_size=sizeof(struct sockaddr_in);
clients[i].sock=accept(sockfd,(struct sockaddr *)&their_addr[i],&sin_size);
send(clients[i].sock,"login :",8,0);
reals=recv(clients[i].sock,buf,64,0);
buf[reals]='\0';
strncpy(clients[i].name, buf, 64 - 1);
printf("%s", buf);
i++;
}
else if(ret==0){
printf("problem?");
}
else{
// Le dernier socket n'a pas de données, on regarde les autres
for(k=0;k<i;k++){
if(FD_ISSET(clients[k].sock,&fd)){
// Données présentes
int j=0;
reals=recv(clients[k].sock,buf,64,0);
// Code foireux de fermeture de socket (condition jamais vraie,
// et que se passe-t-il si le premier client ferme ?)
if(reals==0){
close(clients[k].sock);
i--;
}
else{
buf[reals]='\0';
printf("%s",buf);
memset(msg, 0, 64);
strncpy(msg,clients[k].name,64-1);
strncat(msg,buf,64-strlen(msg)-1);
// envoi des données à tous les clients
for(j=0;j<i;j++){
send(clients[j].sock,msg,64,0);
}
}
}
}
}
}
}
pour le serveur et ici pour le client : Code : #if defined (WIN32)
#include <winsock2.h>
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char** argv){
#if defined (WIN32)
WSADATA WSAData;
int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
#else
int erreur = 0;
#endif
struct sockaddr_in my_addr;
int sockfd;
char buffer[64];
char buf[64];
int reals=0;
int ret=0;
fd_set fd;
sockfd=socket(AF_INET,SOCK_STREAM,0);
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(2300);
my_addr.sin_addr.s_addr=inet_addr("192.168.1.12");
printf("%d",connect(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)));
while(1){
FD_ZERO(&fd);
FD_SET(sockfd,&fd);
if(select(sockfd+1, &fd, NULL, NULL, NULL) == -1){
printf("Select failed on socket I/O");
return -1;
}
if(FD_ISSET(sockfd, &fd)) {
reals=recv(sockfd,buf,64,0);
if(reals==0){
printf("mother fucking problem!!!");
}
printf("%s",buf);
}
fgets(buffer, 64 - 1, stdin);
{
char *p = NULL;
p = strstr(buffer, "\n");
if(p != NULL)
{
*p = 0;
}
else
{
/* fclean */
buffer[64- 1] = 0;
}
}
send(sockfd,buffer,64,0);
}
qui marche chez supersnail mais pas chez moi
|