M
M.Caggiano
Hi!
This code wants to simulate a possible answer of a server, to an
application of connection aside a client.
The server has to accept the connection and send a message with its
name.
The all through socket.
The program DOESN'T HAVE errors or warnings.
I have a problem in the function "bind" (what I have underlined with
of the "- ") because its return value is "-1" (therefore error!).
I don't understand because the function return this value.
Can you help me?
Thank's for all.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <windows.h>
void err_fatal(char *);
main()
{
/* apertura connessione lato client */
int taddr_n, tport_n;
struct sockaddr_in saddr, caddr;
char *buffer;
int result=0,addrlen=0;
SOCKET s,s1;
WSADATA data; // inizializzo la variabile che contiene le primitive
di Winsock
WORD p;
int err=0;
p=MAKEWORD(2,0); // creo la variabile p che contiene la versione
della wsock32.dll
err=WSAStartup(p,&data); // inizializzo la wsock32.dll verificandone
la mancanza di errori
/* creazione socket */
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s==INVALID_SOCKET) err_fatal("socket() failed!");
tport_n=htons(1037);
printf("\nhtons eseguita correttamente(%d)\n",tport_n);
saddr.sin_family=AF_INET;
saddr.sin_port=tport_n;
saddr.sin_addr.s_addr=INADDR_ANY; /* accetto qualsiasi indirizzo
*/
//----------------------------------------------------------------------------------------------------------------------
result=bind(s,(struct sockaddr *) &saddr, sizeof(saddr));
//----------------------------------------------------------------------------------------------------------------------
if(result==-1) err_fatal("Bind() fault");
buffer="My server name is: MIKY";
printf("%s",buffer);
result=listen(s,20);
/* accetto la connessione*/
addrlen=sizeof(struct sockaddr_in);
s1=accept(s,(struct sockaddr *) &caddr, &addrlen);
send(s1,buffer,strlen(buffer),0);
system("pause");
/* Chiude i socket */
WSACleanup();
closesocket(s);
}
/* stampa errore */
void err_fatal(char *mes)
{
printf("%s, errno=%d\n",mes,WSAGetLastError());
perror("");
system("pause");
exit(1);
}
This code wants to simulate a possible answer of a server, to an
application of connection aside a client.
The server has to accept the connection and send a message with its
name.
The all through socket.
The program DOESN'T HAVE errors or warnings.
I have a problem in the function "bind" (what I have underlined with
of the "- ") because its return value is "-1" (therefore error!).
I don't understand because the function return this value.
Can you help me?
Thank's for all.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <windows.h>
void err_fatal(char *);
main()
{
/* apertura connessione lato client */
int taddr_n, tport_n;
struct sockaddr_in saddr, caddr;
char *buffer;
int result=0,addrlen=0;
SOCKET s,s1;
WSADATA data; // inizializzo la variabile che contiene le primitive
di Winsock
WORD p;
int err=0;
p=MAKEWORD(2,0); // creo la variabile p che contiene la versione
della wsock32.dll
err=WSAStartup(p,&data); // inizializzo la wsock32.dll verificandone
la mancanza di errori
/* creazione socket */
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s==INVALID_SOCKET) err_fatal("socket() failed!");
tport_n=htons(1037);
printf("\nhtons eseguita correttamente(%d)\n",tport_n);
saddr.sin_family=AF_INET;
saddr.sin_port=tport_n;
saddr.sin_addr.s_addr=INADDR_ANY; /* accetto qualsiasi indirizzo
*/
//----------------------------------------------------------------------------------------------------------------------
result=bind(s,(struct sockaddr *) &saddr, sizeof(saddr));
//----------------------------------------------------------------------------------------------------------------------
if(result==-1) err_fatal("Bind() fault");
buffer="My server name is: MIKY";
printf("%s",buffer);
result=listen(s,20);
/* accetto la connessione*/
addrlen=sizeof(struct sockaddr_in);
s1=accept(s,(struct sockaddr *) &caddr, &addrlen);
send(s1,buffer,strlen(buffer),0);
system("pause");
/* Chiude i socket */
WSACleanup();
closesocket(s);
}
/* stampa errore */
void err_fatal(char *mes)
{
printf("%s, errno=%d\n",mes,WSAGetLastError());
perror("");
system("pause");
exit(1);
}