Manejo de puertos en C sobre Linux

Por favor si me puedes dar una mano. Quiero manejar puertos con C sobre Linux. No se cual es la sintaxis y que cabecera deberia incluir
Gracias
Respuesta
1
Sockets en C bajo Linux:
Te paso un programa cliente y otro servidor escritos en C. En ellos se abren dos tipos de sockets. De consulta y de escucha:
Client
/* client.c
* Copyright Mark Watson 1999. Open Source Software License.
* Note: derived from NLPserver client example.
* See www.markwatson.com/opensource/opensource.htm for all
* of the natural language server (NLPserver) source code.
*/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
char * host_name = "127.0.0.1"; // local host
int port = 5005;
void main(int argc, char *argv[]) {
char buf[8192];
char message[256];
int socket_descriptor;
struct sockaddr_in pin;
struct hostent *server_host_name;
char *loc;
char string[5] = "QUIT";
int i;
int reply;
char username[20];
if ((server_host_name = gethostbyname(host_name)) == 0) {
perror("Error resolving local host\n");
exit(1);
}
bzero(&pin, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = htonl(INADDR_ANY);
pin.sin_addr.s_addr = ((struct in_addr
*)(server_host_name->h_addr))->s_addr;
pin.sin_port = htons(port);
/*printf("\n\t\t\t1. Logon to the system\n");
printf("\t\t\t2. Enter the system\n\n");
scanf("%d",&reply);
fflush(stdin);
switch(reply)
{
case 1:
{
printf("\t\t\t\nPlease enter a username:- ");
scanf("%s",username);
printf("\n\t\t\t1. Logon to the system\n");
printf("\t\t\t2. Enter the system\n\n");
}
case 2:
{*/
printf("\t\t\t\nPlease enter a username:- ");
gets(username);
printf("YOUR USERNAME IS %s\n",username);
/*To send username*/
if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error opening socket\n");
exit(1);
}
if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
perror("Error connecting to socket\n");
exit(1);
}
if (send(socket_descriptor, username, strlen(username), 0) == -1) {
perror("Error in send\n");
exit(1);
}
close(socket_descriptor);
while(1)
if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error opening socket\n");
exit(1);
}
if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
perror("Error connecting to socket\n");
exit(1);
}
printf("Please enter a message: ");
gets(message);
loc = strstr(message,string);
if (loc != NULL)
exit(1);
printf("Sending message %s to server...\n", message);
if (send(socket_descriptor, message, strlen(message), 0) == -1) {
perror("Error in send\n");
exit(1);
}
printf("..sent message.. wait for response...\n");
if (recv(socket_descriptor, buf, 8192, 0) == -1) {
perror("Error in receiving response from server\n");
exit(1);
}
printf("\nResponse from server:\n\n%s\n", buf);
printf("\nAddress is %d\n",socket_descriptor);
}
/*}*//*End of case 2*/
/*default:
printf("\n\t\t\tINVALID choice please try again\n");
}*/printf("\nAddress 2 is %d\n",socket_descriptor);
close(socket_descriptor);
}
SERVER
/* server.c
* Copyright Mark Watson 1999. Open Source Software License.
*/
#include <errno.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SPACE 32
#define MAXCLIENTS 4
#define UNAMESIZE 20
int port = 5005;
void main() {
struct sockaddr_in myname;
struct sockaddr addr; /* generic socket name */
struct sockaddr_in pin;
struct hostent *hp, *gethostbyaddr(),*host;
struct client
{ char user[UNAMESIZE]; /*Holds user names*/
int socket;
}client_array[MAXCLIENTS];
int sock_descriptor;
int temp_sock_descriptor;
int address_size;
char buf[16384];
int i, n, len,t,len2;
int rc, /* system call return code */
new_sd, sock, /* server/listen socket descriptors */
adrlen, /* sockaddr length */
cnt; /* number of bytes I/O */
sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
if (sock_descriptor == -1) {
perror("call to socket");
exit(1);
}
bzero(&myname, sizeof(myname));
myname.sin_family = AF_INET; /* Internet address */
myname.sin_port = htons(port);
myname.sin_addr.s_addr = INADDR_ANY; /* "Wildcard" */
/* Identify the server process. */
printf("\nThis is the network server");
if (bind(sock_descriptor, &myname, sizeof(myname)) <0) {
close(sock_descriptor); /* defensive programming */
printf("network server bind failure %d\n", errno);
perror("network server");
exit(2);
}
adrlen = sizeof(addr); /* need int for return value */
if ( ( rc = getsockname( sock_descriptor, &addr, &adrlen ) ) < 0 )
{
printf("setwork server getsockname failure %d\n",
errno);
perror("network server");
close (sock);
exit(3);
}
if (listen(sock_descriptor, 20) == -1) {
perror("call to listen");
exit(1);
}
/*temp_sock_descriptor =accept(sock_descriptor, (struct sockaddr *)&pin,
&address_size);
if (temp_sock_descriptor == -1) {
perror("call to accept");
exit(1);
}*/
printf("Accepting connections ...\n");
printf("THIS IS A NAME\n");
while(1) {
/*len2 = sizeof sin;*/
temp_sock_descriptor =accept(sock_descriptor, (struct sockaddr *)&pin,
&address_size);
if (temp_sock_descriptor == -1) {
perror("call to accept");
exit(1);
}
/*close(temp_sock_descriptor);*//* Parent doesn't need work socket. */
/* Fork child process to handle client service request */
if ( ( fork() ) == 0 ) { /* Child process */
int pid;
pid = getpid(); /* PID of child process */
close (sock_descriptor); /* Do not need listen socket in child. */
/* Find out who the client is. Note the use of the
generic address structure addr to hold information
about the (connected) client. */
/* if ((rc = getpeername( temp_sock_descriptor, &addr, &adrlen )) <
0) {
printf("network server %d getpeername failure %d\n",
pid, errno);
perror("network server");
close(temp_sock_descriptor);
exit(6);
}
printf("CLIENT IS %s\n",inet_ntoa(rc));*/
/*if(getpeername(sock_descriptor,(struct sockaddr *) &myname, &len2) < 0)
perror("getpeername");
else {
if ((host = gethostbyaddr((char*) &myname.sin_addr,
sizeof myname.sin_addr,
AF_INET)) == NULL)
perror("gethostbyaddr");
else printf("remote host is '%s'\n",host->h_name);
}*/
n=recv(temp_sock_descriptor, buf, 16384, 0);
if (n==-1)
{
perror("call to recv");
exit(1);
}
printf("received from client:%s\n", buf);
buf[n] =0;
// for this server example, we just convert the
// characters to upper case:
len = strlen(buf);
for (i=0; i<len; i++) buf = toupper(buf);
if (send(temp_sock_descriptor, buf, len, 0) == -1) {
perror("call to send");
exit(1);
}
len = 0;
for(i=0;i<strlen(buf);i++)
buf= SPACE;
exit(0); /* Exit child process */
}/* End of if-child-process true condition */
else /* Not child process; must be parent process */
printf("address IS %d\n",temp_sock_descriptor);
close(temp_sock_descriptor);/* Parent doesn't need work socket. */
}
}
P.D.: Si quieres devolverme el favor, te agradeceré que pongas un link a http://webmaster.bankhacker.com/ayuda-linux/ en tu página web y agradeceré tu rápida y justa valoración de la respuesta de TodoExpertos.com.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas