Client Server C


 
Thread Tools Search this Thread
Top Forums Programming Client Server C
# 1  
Old 06-06-2012
Client Server C

Hello,

Please I would create a client and a server in C that communicate frequently. The client sends "hello message" to the server, the server waits a few minutes and sends a "hello message" to the client, the client sends again "hello server ".. etc up to 15 minutes



Can you guide me please?
Thank you for help
# 2  
Old 06-06-2012
Is this homework?

What have you tried?
# 3  
Old 06-06-2012
Yes it's a homework
I need to change the following code:
Quote:
client.c

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

int socketclient,rc;
int port, retour;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
char adresseserveurip[32];
char buffer[BUFFER_TAILLE];

printf("Entrez l'adresse IP ou le nom du serveur :");
scanf("%s", adresseserveurip);
printf(" port :");
scanf("%d", &port);


h = gethostbyname(adresseserveurip);
if(h==NULL) {
printf("%s: serveur inconnu '%s'\n",argv[0],adresseserveurip);
exit(1);
}


servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(port);


socketclient = socket(AF_INET, SOCK_STREAM, 0);
if(socketclient<0) {
perror("Probleme de creation de socket\n ");
exit(1);
}
//Connexion au serveur
rc = connect(socketclient, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc < 0)
{
printf("Probleme de connexion\n");
exit(1);
}
retour = read (socketclient, buffer, 32);
buffer[32] = '\0';
printf("\nMessage :%s. Nombre de caracteres lus = %d\n", buffer, retour);
close (socketclient); // Fermeture de la socket
return 0;
}


Quote:
server.c

int creer_socket_serveur(int port)



{
int listen_fd;
struct sockaddr_in sin;


if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) exit(1);


sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = INADDR_ANY;


if (bind(listen_fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) exit(1);


listen(listen_fd, 5);

return (listen_fd);
}



main (int argc, char *argv[])
{
int port, so, client_sin_len, nb, socketserveur;
struct sockaddr_in client_sin;
char buffer[BUFFER_TAILLE]="Ceci est un exemple de socket!!!";

port=1111;

socketserveur = creer_socket_serveur(port); //Creation du serveur et socket
printf("Serveur pret ... En attente de requetes ...\n");

client_sin_len = sizeof (client_sin);
so = accept(socketserveur, (struct sockaddr *)&client_sin, &client_sin_len);
if (so == -1)
{
printf ("Probleme de creation d'une socket de traitement\n");
exit (1);
}

nb = write(so, buffer, sizeof (buffer));
printf("Transmission effectuee. Nombre de caracteres transmis = %d\n", nb);
close (so);
close(socketserveur);
exit (0);
}
Thank you so much
# 4  
Old 06-06-2012
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sftp script for dev server to client server

hi, i am new to unix, cuold u send some sftp acripts to send files to dev server to clint server, (1 Reply)
Discussion started by: Koti.annam
1 Replies

2. UNIX for Dummies Questions & Answers

Client server C

Hello, Please, is there on unix.com the source code of a client C and server C: as shown in following figure: File:InternetSocketBasicDiagram zhtw.png - Wikipedia, the free encyclopedia Thank you so much for help (1 Reply)
Discussion started by: chercheur857
1 Replies

3. Windows & DOS: Issues & Discussions

Office server => laptop =>client server ...a lengthy and laborious ftp procedure

Hi All, I need your expertise in finding a way to solve my problem.Please excuse if this is not the right forum to ask this question and guide me to the correct forum,if possible. I am a DBA and on a daily basis i have to ftp huge dump files from my company server to my laptop and then... (3 Replies)
Discussion started by: kunwar
3 Replies

4. Programming

Client/Server Socket Application - Preventing Client from quitting on server crash

Problem - Linux Client/Server Socket Application: Preventing Client from quitting on server crash Hi, I am writing a Linux socket Server and Client using TCP protocol on Ubuntu 9.04 x64. I am having problem trying to implement a scenario where the client should keep running even when the... (2 Replies)
Discussion started by: varun.nagpaal
2 Replies

5. Programming

client and server programs

Hello, Looking at the asio.sourceforge.net library, I found a tutorial to develop simple client and server programs. Below I am pasting the client and the server. On my Fedora Core 4, I can compile the two programs(client is 'a' and server is 'daytime', but I have no idea on how to test them... ... (8 Replies)
Discussion started by: JCR
8 Replies

6. Shell Programming and Scripting

Client-server

Hi, I have installed the vmware server on my debian os and has several clients connected to it. Is there any script that enable the server to restart the client automatically?? Can anyone help. Thanks in advance (3 Replies)
Discussion started by: kanexxx
3 Replies

7. UNIX for Dummies Questions & Answers

Server/client chat

I want to make the following programm. Using the server/client model I want 2 client to connect to the server then the server sends back to the clients the ip address and a number of a poort in order to open a udp connection between clients without using the server? What I have done since now is... (2 Replies)
Discussion started by: kasma
2 Replies

8. AIX

Client/Server Issue

I'm new to the forum and I know just enough about this topic to make me dangerous. This question is probably pretty basic to most of you, but here goes: I've got two servers that I'm connecting to via TCP/IP. The issue is that I'm losing connectivity between the two after a certain period of... (5 Replies)
Discussion started by: geauxtn
5 Replies

9. UNIX for Dummies Questions & Answers

client/server

Hi, yes i belong to that duummies group of people so here is the question that i need someone to explain it to me and posiblly to answere it to me in a plain english. This is the question: Describe the concept of “client-server” software. Discuss what each side of the equation... (2 Replies)
Discussion started by: bole
2 Replies

10. Programming

client server interaction? anyone know

hi there, i need help :( im trying to produce a program that would do the following, but i dont know were to begin, any help/guide lines, please im need help asap, One program is the main program, which fork/execs one referee process and three x,y,z processes repectvley. It then waits until... (0 Replies)
Discussion started by: zmanultra
0 Replies
Login or Register to Ask a Question