473,408 Members | 1,908 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,408 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 17903
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.