Ciao Ananke Melior,
Ho provato sui 2 programmini scritti nell'altro 3d e funziona. A questo punto mi dovresti dire:
1) Come è fatto il file da trasferire. Parli di tabulazioni, scrivi in append, aggiungi la stringa "End" è testuale o cosa?
2) Cosa intendi per "continuare a non funzionare". Non compila? esegue un errrore in run-time? non trasmette? non riceve?
foo:
max@studio:~> ln -s test2.c foo
max@studio:~> ll foo
lrwxrwxrwx 1 max users 7 15 set 10.22 foo -> test2.c
max@studio:~>
client:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#define BLOCK_SIZE 8
#define FAMILY AF_INET
#define PORT 7777
/** get sockaddr, IPv4 or IPv6 */
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
return &(((struct sockaddr_in*)sa)->sin_addr);
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main (int argc, char **argv)
{
int sock,x;
struct addrinfo hints,*servinfo, *p;
char host_addr[INET6_ADDRSTRLEN];
char port[16];
ssize_t len;
char block[BLOCK_SIZE];
if (argc!=2)
{
fprintf (stderr, "Usage:\t%s host\n",argv[0]);
return -1;
}
snprintf (port,sizeof(port),"%d",PORT);
memset(&hints, 0, sizeof (hints));
hints.ai_family =FAMILY;
hints.ai_socktype = SOCK_STREAM;
if ((x=getaddrinfo(argv[1], port, &hints, &servinfo)) != 0)
{
fprintf (stderr,"getaddrinfo: %s\n", gai_strerror(x));
return -1;
}
for(p=servinfo;p;p=p->ai_next)
{
if ((sock=socket(p->ai_family, p->ai_socktype, p->ai_protocol))<0)
{
perror("Warning: socket");
continue;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), host_addr, sizeof (host_addr));
printf ("try connecting to %s port %s...\n",host_addr,port);
if ((connect(sock, p->ai_addr, p->ai_addrlen))<0)
{
perror ("Warning: connect");
continue;
}
printf ("Good one. Done\n");
break;
}
/*
for (;;)
{
fgets (block, BLOCK_SIZE, stdin);
if ((len=send(sock, block,strlen(block),0))<1)
break;
if (strcmp(block,"EXIT\n")==0)
break;
}
*/
/**
*
*
* patching 4U
*
*
*/
#include <errno.h>
#define dwfile_name "foo"
int remoteSocket=sock;
FILE *dwfile;
if ((dwfile=fopen (dwfile_name,"rb"))==NULL)
{
fprintf (stderr,"Cannot read `%s` file\n",dwfile_name);
perror ("Error");
return;
}
//
// Leggo fino a end-of-file o errore
while ((fgets(block,BLOCK_SIZE,dwfile))>0)
{
//
// La dimensione è data dalla lunghezza della stringa
len=strlen(block);
//
// Invio. Se errore esco
if ((send(remoteSocket, block,len,0))!= len)
break;
}
if (errno)
perror ("Uh-oh...");
freeaddrinfo(servinfo);
close (sock);
return 0;
}
server non patchato (basta modificare la
printf con
fprintf):
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BLOCK_SIZE 8
#define FAMILY AF_INET
#define PORT 7777
int main ()
{
int sock_in,client_sock;
struct sockaddr_in addr_in;
ssize_t len;
char block[BLOCK_SIZE];
sock_in=socket(AF_INET,SOCK_STREAM,0);
addr_in.sin_family=FAMILY;
addr_in.sin_addr.s_addr=INADDR_ANY;
addr_in.sin_port=htons(PORT);
if ((bind(sock_in,(struct sockaddr*) &addr_in,sizeof(addr_in)))<0)
perror ("bind");
if ((listen(sock_in,1))<0)
perror ("listen");
printf ("Waiting connection....\n");
if ((client_sock=accept(sock_in,0,0))<0)
perror ("accept");
printf ("done\n");
while ((len=recv(client_sock, block, sizeof(block)-1, 0))>0)
{
block[len]='\0';
printf ("%s",block);
if (strcmp(block,"EXIT\n")==0)
break;
}
if (len<0)
perror ("recv");
else if (len==0)
printf ("connection closed by peer. Bye!\n");
else
printf ("connection closed\n");
close (client_sock);
close (sock_in);
return 0;
}
term:
max@studio:~> gcc server.c -o server
max@studio:~> gcc client.c -o client
max@studio:~> ./server &
[1] 10918
Waiting connection....
max@studio:~> ./client localhost
try connecting to 127.0.0.1 port 7777...
done
Good one. Done
/**
* Copyright (C) 2007, Max Cavallo <ixamit_at_gmail_dot_com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#define DEBUG
#define ETHER_SRC "prod_list.txt"
#define ETHER_DST "prod_list.dat"
int16_t n_prod=0;
unsigned char *prod=NULL;
struct _ethernet_prod
{
unsigned char id[3];
unsigned char prod[64];
};
int build_prod ()
{
struct _ethernet_prod ethernet_prod;
int z[3];
FILE *fp_src,*fp_dst;
if ((fp_src=fopen(ETHER_SRC,"r"))==NULL)
{
fprintf (stderr,"Cannot read file \'%s\'. Abort!",ETHER_SRC);
return 1;
}
if ((fp_dst=fopen(ETHER_DST,"w"))==NULL)
{
fprintf (stderr,"Cannot write file \'%s\'. Abort!",ETHER_DST);
return 1;
}
fwrite (&n_prod,sizeof(int16_t),1,fp_dst);
while (!feof(fp_src))
{
memset (ðernet_prod, 0, sizeof (struct _ethernet_prod));
if ((fscanf (fp_src,"%2X%2X%2X %63[^\n]s",&z[0],&z[1],&z[2],ethernet_prod.prod))>0)
{
ethernet_prod.id[0]=(char)(z[0] & 0xFF);
ethernet_prod.id[1]=(char)(z[1] & 0xFF);
ethernet_prod.id[2]=(char)(z[2] & 0xFF);
fwrite (ðernet_prod,1,sizeof(struct _ethernet_prod),fp_dst);
n_prod++;
#ifdef DEBUG
printf ("%02X-%02X-%02X %s \n",z[0],z[1],z[2],ethernet_prod.prod);
#endif
}
}
fseek(fp_dst,0L, SEEK_SET);
fwrite (&n_prod,sizeof(int16_t),1,fp_dst);
fclose (fp_dst);
fclose (fp_src);
return 0;
}
void free_prod ()
{
if (prod)
{
free (prod);
prod=NULL;
n_prod=0;
}
}
void alloc_prod ()
{
FILE *fp;
int i=0;
if ((fp=fopen(ETHER_DST,"r"))==NULL)
{
fprintf (stderr,"Cannot read file \'%s\'. Abort!",ETHER_DST);
return;
}
fread (&n_prod,sizeof(int16_t),1,fp);
if ((prod=malloc (n_prod*sizeof (struct _ethernet_prod))))
{
while (!feof(fp))
{
if ((fread (prod+i,1,sizeof(struct _ethernet_prod),fp))>0)
i+=sizeof(struct _ethernet_prod);
}
}
fclose (fp);
}
unsigned char *search_prod (char a, char b, char c)
{
char x[3];
int start=0,end=n_prod-1,pivot,r;
struct _ethernet_prod ethernet_prod;
x[0]=a; x[1]=b; x[2]=c;
while (start<=end)
{
pivot=(start+end)/2;
memcpy (ðernet_prod,prod+(pivot*sizeof(struct _ethernet_prod)),sizeof(struct _ethernet_prod));
if ((r=memcmp(ðernet_prod,&x[0],3))==0)
return (prod+(pivot*sizeof(struct _ethernet_prod)))+3;
if (r<0)
start=pivot+1;
else
end=pivot-1;
}
return (unsigned char *)"Unknow";
}
/*******************************************
* MULTICAST ADDRESS
********************************************/
struct _multicast_address
{
unsigned char id[6];
short int len;
unsigned int typefield;
unsigned char prod[64];
};
static struct _multicast_address multicast_address[]=
{
{ "\x01\x00\x0C\xCC\xCC\xCC" ,6, 0x0802,"CDP (Cisco Discovery Protocol), VTP (VLAN Trunking Protocol)"},
{ "\x01\x00\x0C\xCC\xCC\xCD" ,6, 0x0802,"Cisco Shared Spanning Tree Protocol Address"},
{ "\x01\x80\xC2\x00\x00\x00" ,6, 0x0802,"Spanning Tree Protocol (for bridges) IEEE 802.1D"},
{ "\x01\x80\xC2\x00\x00\x08" ,6, 0x0802,"Spanning Tree Protocol (for provider bridges) IEEE 802.1AD"},
{ "\x01\x80\xC2\x00\x00\x02" ,6, 0x8809,"Ethernet OAM Protocol IEEE 802.3ah (A.K.A. \"slow protocols\""},
{ "\x01\x00\x5E" ,3, 0x0800,"IPv4 Nulticast (RFC 1112)"},
{ "\x33\x33" ,2, 0x86DD,"IPv6 Nulticast (RFC 2464)"},
};
unsigned char *search_multicast (unsigned char address[6])
{
int i,end=sizeof(multicast_address)/sizeof(multicast_address[0]);
for (i=0;i<end;i++)
if (!strncmp (multicast_address[i].id,address,multicast_address[i].len))
return multicast_address[i].prod;
return (unsigned char *)"Multicast Unknow";
}
/**
int main ()
{
//build_prod ();
alloc_prod ();
printf ("%s\n",search_prod (0x00,0x24,0x1D));
free_prod ();
return 0;
}
*/
connection closed by peer. Bye!
[1]+ Done ./server
max@studio:~>