Sponsored Content
Full Discussion: Client Server C
Top Forums Programming Client Server C Post 302652207 by chercheur857 on Wednesday 6th of June 2012 05:44:00 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
BINDRESVPORT(3) 					   BSD Library Functions Manual 					   BINDRESVPORT(3)

NAME
bindresvport, bindresvport_sa -- bind a socket to a privileged IP port LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <sys/types.h> #include <rpc/rpc.h> int bindresvport(int sd, struct sockaddr_in *sin); int bindresvport_sa(int sd, struct sockaddr *sa); DESCRIPTION
The bindresvport() and bindresvport_sa() functions are used to bind a socket descriptor to a privileged IP port, that is, a port number in the range 0-1023. If sin is a pointer to a struct sockaddr_in then the appropriate fields in the structure should be defined. Note that sin->sin_family must be initialized to the address family of the socket, passed by sd. If sin->sin_port is '0' then an anonymous port (in the range 600-1023) will be chosen, and if bind(2) is successful, the sin->sin_port will be updated to contain the allocated port. If sin is the NULL pointer, an anonymous port will be allocated (as above). However, there is no way for bindresvport() to return the allo- cated port in this case. Only root can bind to a privileged port; this call will fail for any other users. Function prototype of bindresvport() is biased to AF_INET socket. The bindresvport_sa() function acts exactly the same, with more neutral function prototype. Note that both functions behave exactly the same, and both support AF_INET6 sockets as well as AF_INET sockets. RETURN VALUES
The bindresvport() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. ERRORS
[EPFNOSUPPORT] If second argument was supplied, and address family did not match between arguments. The bindresvport() function may also fail and set errno for any of the errors specified for the calls bind(2), getsockopt(2), or setsockopt(2). SEE ALSO
bind(2), getsockopt(2), setsockopt(2), ip(4) BSD
November 22, 1987 BSD
All times are GMT -4. The time now is 09:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy