Unix: socket & Co


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Unix: socket & Co
# 1  
Old 05-20-2008
Unix: socket & Co

Hello,
I need help to replace the ................. of client.c that request the server implemented by server.c

------------------
Listing 1 - server.c

/* Inclusion des différentes librairies nécessaires */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet.in.h>
#include <arpa.inet.h>

int main(int argc, char *argv[])
{
int res;
/* Operandes */
int a, b,buf;
/* socket mise en place côté serveur */
int serv_sock;
/* socket mise en place côté client */
int cli_sock;
/* adresse de la socket distante */
struct sockaddr_in localaddr;
/* longueur de l'adresse */
socklen_t localaddr_len;
/* longueur de l'adresse */
socklen_t distaddr_len;
localaddr_len = sizeof(localaddr);

/* Mise en place de la socket */
serv_sock = socket(AF_INET.SOCK_STREAM, 0);
if (serv_sock = socket(AF_INET.SOCK_STREAM, 0);
if (serv_sock == -1) {
perror(''Impossible de creer la socket'')
exit(-1);
}
/* Mise en place de la structure décrivant l'adresse
* et le port qui seront associés à la socket */
bzero((char*)&localaddr, localaddr_len);
/* Protocole internet */
localaddr.sin_family = AF.INET;
/* On écoute sur l'adresse de boucle locale */
localaddr.sin_addr.s_addr = inet_addr(''127.0.0.1'');
/* On écoute sur le port 1234 */
localaddr.sin_port=htons(1234);

/* On attache la socket au port 1234 sur
* l'adresse locale 127.0.0.1 */
if (bind(serv_sock,
(struct sockaddr *) &localaddr,
localaddr_len) == -1) {
perror(''Impossible d'attacher la socket'');
exit(-1);
}
/* Mise en place de l'écoute sur la socket */
if (listen(serv_sock, 1) == -1) {
perror(''Impossible d'écouter sur la socket'');
exit(-1);
}
/* On accepte les connexions entrantes */
cli_sock = accept(serv_sock,
(struct sockaddr *) &distaddr,
&distaddr_len);
if (cli_sock == -1) {
perror(''Erreur de connexion du client'');
exit(-1);
}
/* on recoit la première operande */
res = recv(cli_sock, &buf; sizeof(int), 0);
if (res != sizeof(int)) {
perror(''Donnees recues incorrectes'');
close(serv_sock);
exit(-1);
}
a = ntohl(buf);
printf(''Recu: %d/n'', a);

/* On recoit la deuxieme operande */
res = recv(cli_sock, &buf, sizeof(in), 0);
if (res != sizeof(int)) {
perror(''Donnees recues incorrectes'');
close(serv_sock);
exit(-1);
}
b = ntohl(buf);
printf(''Recu: %d\n'', b);

buf=htonl(a+b);
printf(''Envoi: %d\n'', a+b);
/* On envoie le resultat */
res=send(cli_sock, &buf, sizeof(int), 0);
if (res != sizeof(int)) {
perror(''Erreur de transmission de donnees'');
close(serv_sock);
exit(-1)
}
close(serv_sock);
close(cli_sock);
return ();
}
-----------------
Listing 2 - client.c

// Inclusion des différentes librairies necessaires
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
int res;
/* Operandes */
int a, b, buf;
/* socket mise en place cote serveur */
int serv_sock;
/* socket mise en place cote client */
int cli_sock;
/* adresse de la socket distante */
struct sockaddr_in distaddr;
/* longueur de l'adresse */
socklen_t distaddr_len;
distaddr_len = sizeof(distaddr);

a=htonl(123);
b=btonl(124);

/* Creation de la socket */
cli.sock = .............................
if (cli_sock == -1) {
perror(''Impossible de creer la socket'');
exit(-1);
}
/* Mise en place de la structure decrivant
* l'adresse et le port qui seront associes
* a la socket */
bzero((char*)&distaddr, distaddr_len);
/* Protocole internet */
distaddr.sin_family = .......................
/* On se connecte sur l'adresse de boucle locale */
distaddr.sin_addr.s_addr = inet_addr(''127.0.0.1");
/* On se connecte sur le port 1234 */
distaddr.sin.port = .............................
/* Connexion au serveur */
serv_sock = ....................................
if (serv_sock == -1) {
perror(''Impossible de se connecter'');
}
/* On envoi la premiere operande */
res = .....................................
if (res != sizeof(int)) {
perror(''Impossible d'envoyer les donnees'');
close(cli_sock);
exit(-1);
}

/* On envoie la seconde operande */
res = send(cli_sock, &b, sizeof(int), 0);
if (res != sizeof(int)) {
perror(''Impossible d'envoyer les donnees'');
close(cli_sock);
exit(-1);
}
/* On recupere le resultat */
res = ....................................
if (res != sizeof(int)) {
perror(''Impossible de lire les donnees'');
close(cli_sock);
exit(-1);
}
printf(''Resultat:%d\n'', ntohl(buf));
.............................
close(serv_sock);
return 0;
}
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

C++ socket, fork & pipes

Hello, I'm stuck and this is a matter which I need to resolve quite fast (but I couldn't post in the "Emergency" section); the problem is this : I have created a chat program in which the client sends the sentence to the server and then the server should send it to all the clients connected,... (2 Replies)
Discussion started by: timmyyyyy
2 Replies

2. Programming

Unix Socket

Hai every one ,,, I am runinng one tcp socket in non-blocking mode. In the connect state(operation in progress) if the server is not running ,client socket select will give success. FD_ISSET(socketfd,&wfd) will give me succes always and am getting wfd value as 16. I tried with blocking... (6 Replies)
Discussion started by: andrew.paul
6 Replies

3. Programming

unix socket programming

Hi all, my name is anjali, working as an intern, doing masters i have an assignment from my school, which i am not able to solve, can someone please help me in finding the solution for this problem thanking you in advance....lol Write a C socket program on UNIX where a TCP client... (1 Reply)
Discussion started by: anjali.symantec
1 Replies

4. UNIX for Advanced & Expert Users

C socket program on UNIX

Write a C socket program on UNIX where a TCP client sends/reads a time in decimal 24 hours format to the server and the server echoes the seconds, minutes, and hours in the time. Example: Client sends 18.78 hours and the server displays 18 hours, 46 minutes and 48 seconds. (1 Reply)
Discussion started by: adi_always4u143
1 Replies

5. IP Networking

Help with Unix socket programing

hi I am strucked in a client server program client need to login to server client logins if only username and password are correct i have written a program username is stored as file and password is smilar to username whic is stored in that file when server asks for username... (2 Replies)
Discussion started by: karthik1238
2 Replies

6. UNIX for Dummies Questions & Answers

Socket Handling Differences Between Linux & Unix?

Sorry if this is a stupid question! I have been developing a Java application that I am deploying on both Unix and Linux servers, which uses lots of socket handling. When the server side connection is dropped by the server un-gracefully I have been seeing close_waits and null connections. ... (0 Replies)
Discussion started by: Vinnie
0 Replies

7. Programming

regarding socket & mssage queue

hello , I have to write an application in which I had to implement both Socket Comminication and IPC- message queues. and that process should run in Infinite loop as well I had to continously check and send data through both type of communications... What should I use to implement it... I had... (34 Replies)
Discussion started by: arunchaudhary19
34 Replies

8. Programming

How can I program socket in unix?

Excuse me . I'm a beginner . In windows , MFC can be used , but how to do in Unix ? And does unix support c++like VC++ ? How can I get developing tools in Unix ? (7 Replies)
Discussion started by: sanjohn
7 Replies

9. UNIX Desktop Questions & Answers

what is the difference between Unix & linux, what are the advantages & disadvantages

ehe may i know what are the difference between Unix & Linux, and what are the advantages of having Unix as well as disadvantages of having Unix or if u dun mind i am dumb do pls tell me what are the advantages as well as the disadvantages of having linux as well. thanks (1 Reply)
Discussion started by: cybertechmkteo
1 Replies
Login or Register to Ask a Question