473,236 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 software developers and data experts.

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 17883
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
6
by: Clarence Gardner | last post by:
I've got a problem that I don't see how to program around. A socket server that was running fine, today started getting an exception from the bind() call (errno 22, Invalid argument) and yet the...
2
by: Rookie | last post by:
Hi, I was writing a simple program using sockets. The program is supposed to be a TCP server that receives a string sent by the client. For this purpose I defined a char array called readString...
2
by: priya | last post by:
Hi All, Currently I am working in Socket Progrm written in C. Here i pasted Both Server and client code.. Server.c code
2
by: Rene Sørensen | last post by:
We are 4 students working on a assignment, that our teacher gave use, normally we do this is C++, but the 4 of us, use C# more often that C++ so… We made a small games called reversi, now our job...
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
0
by: Mangabasi | last post by:
Howdy, I would like to use the Synthesis Toolkit for a demo. I downloaded the STK from http://ccrma.stanford.edu/software/stk/index.html. It seems very powerful and user friendly. There are...
3
by: Clement | last post by:
Please help me....... I am getting blocked in bind() system call....... i don't know why can you please any one tell me why........ #include<stdio.h> #include<sys/un.h>
2
by: Jean-Paul Calderone | last post by:
On Mon, 12 May 2008 11:16:08 -0700 (PDT), petr.poupa@gmail.com wrote: I'm not sure if you need to write a server or a client. In your original code, you had a client which repeatedly established...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.