![]() |
|
|
google unix.com
|
|||||||
| Forums | S'inscrire | Forum Rules | Liens | Albums | FAQ | Liste des membres | Calendrier | Recherche | Aujourd'hui, les postes | Marquer les forums comme lus |
| High Level Programming Posez vos questions à propos de C, C + +, Java, SQL, et d'autres langages de programmation ici. |
Plus d'UNIX et Linux Forum Sujets Vous trouverez peut-être utile
|
||||
| Fil | Thread Starter | Forum | Réponses | Last Post |
| Serveur client | pip3r | High Level Programming | 9 | 04-17-2008 02:15 AM |
| Client et serveur gen de Makefile | wongalan48 | High Level Programming | 0 | 03-05-2007 01:09 PM |
| Chat client-serveur | powermind | High Level Programming | 1 | 09-04-2006 12:19 PM |
| programme de transfert d'un fichier de l'ordinateur client au serveur | nathgopi214 | High Level Programming | 3 | 07-04-2006 07:16 AM |
| Je veux un programme de résidence du client UDP | Nirmala | IP Networking | 1 | 06-10-2005 04:46 PM |
![]() |
|
|
LinkBack | Thread Tools | Recherche sur ce Thread | Rate Thread | Modes d'affichage |
|
|
|
||||
|
Client - serveur
Je suis venu ACORS ce codage lors de la net.this surfin code fonctionne perfectly.but que je suis de nouveau à cette prise de programmation i need sm commentaires sur la cité ou d'explication quant à ce code source. J'ai entendu le prb server.ci ont affiché ci-dessous can u guys help me! cheerZ Le server.c Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#define MAX_CLIENTS 64
struct sockaddr_in servaddr;
int addr_len;
struct sockaddr addr;
struct sockaddr_in *ptr;
int cl_addr_len;
struct sockaddr_in cl_addr;
char in_buf[100], out_buf[100];
char names [10][30];
char message[150];
int num_names = 0;
typedef struct cl_info
{
char cl_id[30];
int cl_status;
}cl_info;
cl_info clients[MAX_CLIENTS];
int main(int argc, char **argv)
{
int newsockfd;
int sockfd;
int nbytes;
int max_fd;
int fd;
short func_num;
fd_set test_set, ready_set;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
servaddr.sin_family - AF_INET;
servaddr.sin_port = 0; /*choose an unused port at random */
servaddr.sin_addr.s_addr = INADDR_ANY; /* auto fill with my IP address */
bind(sockfd,(struct sockaddr*) &servaddr, sizeof(servaddr));
addr_len = sizeof(addr);
getsockname(sockfd, &addr, &addr_len);
ptr = (struct sockaddr_in *)&addr;
printf("\nServer on port# %d\n", ptr->sin_port);
listen(sockfd, 5);
max_fd = sockfd;
FD_ZERO(&test_set); /*clear the listening socket */
FD_SET(sockfd, &test_set);
while(1)
{
memcpy(&ready_set, &test_set, sizeof(test_set));
select(max_fd+1, &ready_set, NULL, NULL, NULL);
if(FD_ISSET(sockfd, &ready_set)) /*if listening sock has data its a call from new client */
{
/*create a new socket */
cl_addr_len = sizeof(cl_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cl_addr, &cl_addr_len);
printf("\nA new connection has been made to socket %d\n", newsockfd);
FD_SET(newsockfd, &test_set); /* add new socket to list */
if(newsockfd > max_fd)
max_fd = newsockfd; /*update max_fd with new socket file discriptor */
} /*end if*/
for(fd=0; fd <= max_fd; fd++) /*loop through connected socket file discriptors */
{
if((fd !=sockfd) && FD_ISSET(fd, &ready_set))/*bypass listening sock if sock has data*/
if(process_call(fd, max_fd) == 0) /*if socket is closed */
{
printf("\nThe listening socket is now closed\n");
close(newsockfd);
printf("\nClient on socket %d has left the room\n", sockfd);
FD_CLR(fd, &test_set); /*remove socket from list */
/*decrement user count */
} /*end if */
} /* end for loop */
} /*end while loop */
return 0;
} /*end main */
int add_name(int sockfd)
{
int nbytes;
short name_len;
short n_name_len;
printf("\nAdding name .....\n");
nbytes = read(sockfd, (char *)&name_len, sizeof(short));
name_len = ntohs(name_len); /*convert name_len from network to host short */
memset(clients[sockfd].cl_id, 0, 30); /*set all bytes to zero */
nbytes = read(sockfd, clients[sockfd].cl_id, name_len);
num_names ++; /*increment user count */
printf("\n%s has joined the chat room\n", clients[sockfd].cl_id);
/*need to broadcast user has joined plus user id to connected clients here */
} /*end add_name function */
int how_many(int sockfd, int max)
{
int sock;
short user_len;
char username[30];
for(sock = 4; sock <= max; sock++)
{
strcpy(username, clients[sock].cl_id);
user_len = htons(strlen(username));
write(sockfd, (char *)&user_len, sizeof(short));
write(sockfd, username, strlen(username));
send_user_list(sockfd, username);
}
} /*end how_many function */
int send_user_list(int sockfd, char *users)
{
short msg_type;
short user_len;
msg_type = htons(2);
printf("msg_type is : %u\n", msg_type);
write(sockfd, &msg_type, sizeof(short));
user_len = htons(strlen(users));
write(sockfd, (char *)&user_len, sizeof(short));
write(sockfd, users, strlen(users));
}
int find_name(int sockfd, int max)
{
char search_name[30];
int sock;
int found = 0;
short len, name_len;
char sender[30];
short sender_len;
char msg[150];
short msg_len;
memset(search_name, 0, 30); /*set all bytes to zero */
printf("\nReceiving userid......\n");
read(sockfd, (char *)&name_len, sizeof(short));
len = ntohs(name_len);
read(sockfd, search_name, len);
read(sockfd, &msg_len, sizeof(short));
msg_len = ntohs(msg_len);
read(sockfd, msg, msg_len);
msg[msg_len] = '\0';
strcpy(sender, clients[sockfd].cl_id);
for(sock = 4; sock <= max; sock++)
{
printf(" %d is held in max\n", max);
printf("\ncomparing search request for %s with %s username\n",
search_name, clients[sock].cl_id);
if(strcmp(search_name, clients[sock].cl_id) ==0)
{
printf("\nuserid found\n");
found = 1;
send_async_msg(sock, msg,sender);
}/*end if */
} /*end for loop */
if(! found)
{
printf("\nuserid not found\n");
} /*end if */
}/*end find_name function */
int process_call(int newsockfd, int max_fd)
{
int nbytes;
short func_num;
nbytes = read(newsockfd, (char *)&func_num, sizeof(short));
if(nbytes ==0)
return 0;
if (nbytes != sizeof(short))
{
perror("\nUnknown menu option!\n");
exit(1);
} /* end if */
func_num = ntohs(func_num);
switch(func_num)
{
case(1):
add_name(newsockfd);
break;
case(2):
how_many(newsockfd, max_fd);
break;
case(3):
find_name(newsockfd, max_fd);
break;
case(4):
send_global_msg(newsockfd, max_fd);
break;
case(5):
printf("\nShutting down Server...\n");
exit(0);
default:
printf("\nUnknown function number!\n");
break;
} /*end switch */
} /*end process_call function */
int send_async_msg(int sockfd, char * msg, char *sender)
{
short msg_type;
short msg_len;
short sender_len;
msg_type = htons(1);
printf("msg_type is : %u\n", msg_type);
write(sockfd, &msg_type, sizeof(short));
msg_len = htons(strlen(msg));
write(sockfd, (char *)&msg_len, sizeof(short));
write(sockfd, msg, strlen(msg));
sender_len = htons(strlen(sender));
write(sockfd, (char *)&sender_len, sizeof(short));
write(sockfd, sender, strlen(sender));
}/* end of send_async_msg function */
int send_global_msg(int sockfd, int max_fd)
{
int nbytes;
int sock;
short len;
short msg_type;
char msg[150];
char sender[30];
short sender_len;
strcpy(sender, clients[sockfd].cl_id);
msg_type = 1;
memset(in_buf, 0, sizeof(in_buf)); /*set all to zero */
nbytes = read(sockfd, &len, sizeof(short));
len = ntohs(len);
read(sockfd, msg, len);
message[len] = '\0';
for(sock = 4; sock <= max_fd; sock++)
{
send_async_msg(sock, msg, sender);
} /*end for loop */
} /* end of send_global_msg function */
Dernière édition par reborg; au 09.16.2006 04:51 PM.. |
|
||||
|
s'il vous plaît lire mon post
Citation:
Merci. |
|
||||
|
Bonjour
Citation:
Bonjour, merci. Vous êtes tous prêts donne-moi de répondre au sujet de l'extraction de la chaîne, puis de l'imprimer. Je pensais que personne ne voyait. À l'avenir, je vais attendre un long moment, avant de demander à nouveau. Ma question est de savoir comment extraire une chaîne spécifique dans un fichier avec sscanf () et ensuite imprimant. J'ai eu la réponse. Mais maintenant je me demande si il est possible d'extraire plus d'une corde de plus d'une ligne à partir d'un fichier. Dis, nous avons un fichier, cat / proc / cpuinfo processor: 0 vendor_id: GenuineIntel cpu family: 15 modèle: 2 model name: Intel (R) Celeron (R) CPU 2.60GHz stepping: 9 cpu MHz: 2591.654 cache size: 128 KB fdiv_bug: pas de hlt_bug: pas de f00f_bug: pas de coma_bug: pas de fpu: yes fpu_exception: oui CPUID: 2 wp: yes flags: fpu vme de pse Et je tiens à extraire vendor_id: GenuineIntel modèle: 2 model name: Intel (R) Celeron (R) CPU 2.60GHz Je sais que cela est possible par le code suivant wrting trois fois dans ce cas que nous sommes ici l'extraction de trois cordes. if (NULL \u003d\u003d (fp \u003d fopen (argv [1], "rt "))){ printf ( "Impossible d'ouvrir% s \ n", argv [1]); sortie (1); ) for (; fgets (buf, 255, fp) (if (buf \u003d\u003d strstr (buf, "nom de modèle")) ( if (NULL! \u003d (p \u003d strchr (buf,':'))) printf ( "% s \ n", p + 1); break; ) Mais y at-il un moyen, pour lequel je peux écrire le code une fois, mais peut extraire quelques chaînes de caractères et de les imprimer à la fois l'un après l'autre. Et aussi, est-il un moyen, de sorte que, dans un fichier C mutiple je peux ouvrir les fichiers en argument de ligne de commande, l'un après l'autre. Dire, après je suis fait à l'impression vendor_id, le type de modèle, et le nom du modèle de cpuinfo fichier de répertoire / proc, j'ai ouvert la stat fichier à partir du répertoire / proc et de commencer l'extraction de certaines chaînes de caractères et l'impression à nouveau. Est-ce une manière ou d'compilcated très très simple. S'il vous plaît laissez-moi savoir comment faire si vous savez. Et aussi, quand on extrait une chaîne de caractères, par exemple, cpu family: 15, 15 est un type string. Dis, je veux convertir chaîne 15 à un nombre entier, donc, je l'utilise atoi (), mais de l'imprimer, je dois faire ce qui suit à droite? int number \u003d atoi (p); printf ( "La valeur est:% d \ n", nombre); Mais bon, je ne peux pas imprimer la valeur. Donc, je pense que je manque quelque chose. Quelle que soit je peux aider, je serai très reconnaissant. Merci. ![]() |
![]() |
| Bookmarks |
| Thread Tools | Recherche sur ce Thread |
| Modes d'affichage | Rate this thread |
|
|