468,513 Members | 1,854 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Socket: bind error...why??

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);
}
Feb 3 '08 #1
2 17430
In article <2e**********************************@z17g2000hsg. googlegroups.com>,
M.Caggiano <Mi**************@gmail.comwrote:
>This code wants to simulate a possible answer of a server, to an
application of connection aside a client.
>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?
bind() is not part of the standard C library. As you appear to
be using Windows, you should be asking in a Windows programming
newsgroup.

WSADATA data; // inizializzo la variabile che contiene le primitive
di Winsock
err=WSAStartup(p,&data); // inizializzo la wsock32.dll verificandone
la mancanza di errori
You are possibly using the value of data before it is initialized,
unless WSAStartup (whatever that is) happens to be storing a value
in through the pointer that you are providing to it.
tport_n=htons(1037);
result=bind(s,(struct sockaddr *) &saddr, sizeof(saddr));
What will happen if the port is already in use?
buffer="My server name is: MIKY";
printf("%s",buffer);
You do not appear to be flushing the buffer, and you are not
ending your buffers with \n . Possibly whatever you print is not
being sent until something later flushes the buffer, such as the
termination of the program. Consider using fflush(stdout).
>void err_fatal(char *mes)
{
printf("%s, errno=%d\n",mes,WSAGetLastError());
perror("");
With your placement of perror() before the printf(), the
perror() could be reflecting an error result found in the
printf() routine rather than whatever error triggered you
calling err_fatal().
> system("pause");
exit(1);
}

--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec
Feb 3 '08 #2
On 3 Feb, 23:45, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <2e147501-6f15-42ea-8aeb-f2d17ff28...@z17g2000hsg.googlegroups.com>,

M.Caggiano <Michele.Caggi...@gmail.comwrote:
This code wants to simulate a possible answer of a server, to an
application of connection aside a client.
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?

bind() is not part of the standard C library. As you appear to
be using Windows, you should be asking in a Windows programming
newsgroup.
WSADATA data; // inizializzo la variabile che contiene le primitive
di Winsock
err=WSAStartup(p,&data); // inizializzo la wsock32.dll verificandone
la mancanza di errori

You are possibly using the value of data before it is initialized,
unless WSAStartup (whatever that is) happens to be storing a value
in through the pointer that you are providing to it.
tport_n=htons(1037);
result=bind(s,(struct sockaddr *) &saddr, sizeof(saddr));

What will happen if the port is already in use?
buffer="My server name is: MIKY";
printf("%s",buffer);

You do not appear to be flushing the buffer, and you are not
ending your buffers with \n . Possibly whatever you print is not
being sent until something later flushes the buffer, such as the
termination of the program. Consider using fflush(stdout).
void err_fatal(char *mes)
{
printf("%s, errno=%d\n",mes,WSAGetLastError());
perror("");

With your placement of perror() before the printf(), the
perror() could be reflecting an error result found in the
printf() routine rather than whatever error triggered you
calling err_fatal().
system("pause");
exit(1);
}

--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec
Thank's so much for the printf():).

however i've resolved the problem of the "bind()".
All it took is changing number of port.
Thank's so much!!!
Feb 3 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by anuradha.k.r | last post: by
6 posts views Thread by Clarence Gardner | last post: by
2 posts views Thread by Rookie | last post: by
2 posts views Thread by priya | last post: by
2 posts views Thread by Rene Sørensen | last post: by
2 posts views Thread by Vitali Gontsharuk | last post: by
reply views Thread by Mangabasi | last post: by
3 posts views Thread by Clement | last post: by
2 posts views Thread by Jean-Paul Calderone | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.