En este ejemplo, lo que el cliente recibe por la entrada estándar, es enviado a través de la conexión, al servidor.Socket con conexión
Para
compilar: gcc
-o cliente cliente.c
Para ejecutar el cliente: cliente host
puerto
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
extern int errno;
char bufer[256];
main(int argc, char * argv[]){
int conexion, l;
struct sockaddr_in client;
struct hostent *hp, *gethostbyname();
bzero(&client, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(atoi(argv[2]));
if (isdigit(argv[1][0]))
client.sin_addr.s_addr = inet_addr(argv[1]);
else {
hp = gethostbyname(argv[1]);
if (hp == NULL) {
perror ("¿Cual host ? ");
exit (1);
}
bcopy(hp->h_addr, &client.sin_addr, hp->h_length);
}
/*******************************************************************/
conexion = socket(AF_INET, SOCK_STREAM, 0);
if (conexion < 0) {
perror ("no es posible crear socket");
exit (1);
}
/*******************************************************************/
connect(conexion, (struct sockaddr *)&client, sizeof(client));
while((l=read(0, bufer, sizeof(bufer)))>0) write(conexion, bufer, l);
}
Para compilar: gcc -o servidor servidor.c
Para ejecutar el servidor: servidor puerto
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
extern int errno;
char bufer[256];
main(int argc, char * argv[]){
struct sockaddr_in server;
int x,l,adrl, sock, conexion;
/********************************************************************/
sock= socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror ("no se puede crear socket");
exit (1);
}
/********************************************************************/
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(atoi(argv[1]));
x = bind(sock, (struct sockaddr *)&server, sizeof(server));
if (x<0){
close(sock);
perror("no se puede hacer bind del socket");
exit(1);
}
/********************************************************************/
if (listen(sock, 5) < 0) {
perror ("no listen");
exit (1);
}
/********************************************************************/
adrl = sizeof (struct sockaddr_in);
printf("Servidor activo\n");
while(1){
conexion = accept(sock, (struct sockaddr *)&server, &adrl);
printf("Llama IP: %s\n",inet_ntoa(server.sin_addr));
while((l=read(conexion, bufer, sizeof(bufer)))>0) write(1, bufer, l);
printf("Fin de la llamada\n");
close(conexion);
}
}
Para compilar: gcc -o servidor servidor.c
Para ejecutar el servidor: servidor
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
extern int errno;
char bufer[256];
main(int argc, char * argv[]){
struct sockaddr_in server;
int x, l, adrl, sock, conexion;
/********************************************************************/
sock= socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror ("no se puede crear socket");
exit (1);
}
/********************************************************************/
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = 0; //si se pone cero significa que el sistema operativo asigna No:
x = bind(sock, (struct sockaddr *)&server, sizeof(server));
if (x<0){
close(sock);
perror("no se puede hacer bind del socket");
exit(1);
}
adrl = sizeof (struct sockaddr_in);
getsockname(sock, (struct sockaddr *)&server, &adrl);
printf("Me asignaron el puerto: %d\n",ntohs(server.sin_port));
/********************************************************************/
if (listen(sock, 5) < 0) {
perror ("no listen");
exit (1);
}
/********************************************************************/
printf("Servidor activo\n");
while(1){
conexion = accept(sock, (struct sockaddr *)&server, &adrl);
printf("Llama IP: %s\n",inet_ntoa(server.sin_addr));
while((l=read(conexion, bufer, sizeof(bufer)))>0) write(1, bufer, l);
printf("Fin de la llamada\n");
close(conexion);
}
}
Para compilar: gcc -o servidor servidor.c
Para ejecutar el servidor: servidor segundos
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
extern int errno;
char bufer[256];
main(int argc, char * argv[]){
struct sockaddr_in server;
int x, l, adrl, sock, conexion;
/********************************************************************/
sock= socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror ("no se puede crear socket");
exit (1);
}
/********************************************************************/
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
extern int errno;
char bufer[256];
main(argc,argv)
int argc;
char *argv[];
{
struct sockaddr_in server;
int x, l, adrl, sock, conexion;
/********************************************************************/
sock= socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror ("no se puede crear socket");
exit (1);
}
/********************************************************************/
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = 0;
x = bind(sock, (struct sockaddr *)&server, sizeof(server));
if (x<0){
close(sock);
perror("no se puede hacer bind del socket");
exit(1);
}
adrl = sizeof (struct sockaddr_in);
getsockname(sock, (struct sockaddr *)&server, &adrl);
printf("Me asignaron el puerto: %d\n",ntohs(server.sin_port));
/********************************************************************/
if (listen(sock, 5) < 0) {
perror ("no listen");
exit (1);
}
/********************************************************************/
/**********************TIMEOUT*******************************/
{
struct timeval t;
int result;
int ol = sizeof(struct timeval);
result = getsockopt(sock,
SOL_SOCKET,
SO_RCVTIMEO,
&t,
&ol);
if (result < 0)perror("timeout");
printf("el timeOut era: %d\n", t.tv_sec);
t.tv_sec = atoi(argv[1]); /* se asigna 10 segundos de timeOut */
t.tv_usec = 0;
result = setsockopt(sock, /* socket o conexion afectado */
SOL_SOCKET, /* opcion al nivel TCP */
SO_RCVTIMEO, /* nombre de la opcion */
&t, /* tiempo */
sizeof(struct timeval)); /* largo de la estructura */
if (result < 0)perror("timeout");
result = getsockopt(sock,
SOL_SOCKET,
SO_RCVTIMEO,
&t,
&ol);
if (result < 0)perror("timeout");
printf("el timeOut ahora es: %d\n", t.tv_sec);
}
/******************TIMEOUT****************/
printf("Servidor activo\n");
while(1){
printf("Esperando conexion\n");
conexion = accept(sock, (struct sockaddr *)&server, &adrl);
if (conexion <0)printf("Timeout en conexion \n");
else {
printf("Llama IP: %s\n",inet_ntoa(server.sin_addr));
{
struct timeval t;
int result;
int ol = sizeof(struct timeval);
t.tv_sec = atoi(argv[2]); /* se asigna 10 segundos de timeOut */
t.tv_usec = 0;
result = setsockopt(conexion, /* socket o conexion afectado */
SOL_SOCKET, /* opcion al nivel TCP */
SO_RCVTIMEO, /* nombre de la opcion */
&t, /* tiempo */
sizeof(struct timeval)); /* largo de la estructura */
if (result < 0)perror("timeout");
}
while(1){
l=recv(conexion, bufer, sizeof(bufer),0);
if (l==0){
printf("fin de llamada\n");
break;
}
if (l>0) {write(1, bufer, l);if(bufer[0]=='A')break;}
if (l<0){
printf("timeOut recepcion!!!\n");
//break;
}
}
close(conexion);
}
}
}
server.sin_port = 0;
x = bind(sock, (struct sockaddr *)&server, sizeof(server));
if (x<0){
close(sock);
perror("no se puede hacer bind del socket");
exit(1);
}
adrl = sizeof (struct sockaddr_in);
getsockname(sock, (struct sockaddr *)&server, &adrl);
printf("Me asignaron el puerto: %d\n",ntohs(server.sin_port));
/********************************************************************/
if (listen(sock, 5) < 0) {
perror ("no listen");
exit (1);
}
/********************************************************************/
printf("Servidor activo\n");
while(1){
conexion = accept(sock, (struct sockaddr *)&server, &adrl);
printf("Llama IP: %s\n",inet_ntoa(server.sin_addr));
/**********************TIMEOUT*******************************/
{
struct timeval t;
int result;
int ol = sizeof(struct timeval);
result = getsockopt(conexion,
SOL_SOCKET,
SO_RCVTIMEO,
&t,
&ol);
if (result < 0)perror("timeout");
printf("el timeOut era: %d\n", t.tv_sec);
t.tv_sec = atoi(argv[1]); /* se asigna 10 segundos de timeOut */
t.tv_usec = 0;
result = setsockopt(conexion, /* socket o conexion afectado */
SOL_SOCKET, /* opcion al nivel TCP */
SO_RCVTIMEO, /* nombre de la opcion */
&t, /* tiempo */
sizeof(struct timeval)); /* largo de la estructura */
if (result < 0)perror("timeout");
result = getsockopt(conexion,
SOL_SOCKET,
SO_RCVTIMEO,
&t,
&ol);
if (result < 0)perror("timeout");
printf("el timeOut ahora es: %d\n", t.tv_sec);
}
/******************TIMEOUT****************/
while(1){
l=read(conexion, bufer, sizeof(bufer));
if (l==0){
printf("fin de llamada\n");
break;
}
if (l>0) write(1, bufer, l);
if (l<0){
printf("timeOut!!!\n");
break;
}
}
close(conexion);
}
}