socket - bind

CSocket sock;
 if(sock.Create(0,SOCK_DGRAM,0) == false)
 {
  AfxMessageBox("Create");
  return;
 }
 UINT portnr = 5000;
 if (sock.Bind(portnr,"10.0.0.10") == false)
 {
  int error = sock.GetLastError();
  AfxMessageBox("Bind");
  return;
 }
When running this code I get an error 10022.
According to msdn.microsoft.com it means :
10022
An invalid argument was supplied.
WSAEINVAL

What am I doing wrong ?


0
b7490 (14)
1/15/2004 8:13:39 PM
vc.mfc 33608 articles. 0 followers. Follow

2 Replies
1154 Views

Similar Articles

[PageSpeed] 37

Minor detail: it is silly to write "== false" because it might fool you into thinking that
the opposite is "== true", which is not always the case.  Change it to
	if(!sock.Bind(portnr, SOCK_DGRAM);

Unless you are running on a machine with multiple network cards, you should not specify
the network address. If you are, you must not hardwire the address.

I would suspect that the use of the explicit IP address may be leading to the error; I
create sockets all the time, never use the third parameter, and never get that error. 
					joe

On Thu, 15 Jan 2004 21:13:39 +0100, "earl" <b@email.com> wrote:

>CSocket sock;
> if(sock.Create(0,SOCK_DGRAM,0) == false)
> {
>  AfxMessageBox("Create");
>  return;
> }
> UINT portnr = 5000;
> if (sock.Bind(portnr,"10.0.0.10") == false)
> {
>  int error = sock.GetLastError();
>  AfxMessageBox("Bind");
>  return;
> }
>When running this code I get an error 10022.
>According to msdn.microsoft.com it means :
>10022
>An invalid argument was supplied.
>WSAEINVAL
>
>What am I doing wrong ?
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
1/17/2004 8:24:16 PM
Joseph M. Newcomer wrote:
> Minor detail: it is silly to write "== false" because it might fool you into thinking that
> the opposite is "== true", which is not always the case.

Shouldn't compiler take care of this? After all bool is internal type. 
But even if it is true what you've said, shouldn't we then always 
compare with false and never with true?

0
mac1 (347)
1/19/2004 9:32:07 AM
Reply:

Similar Artilces:

SOCKET #3
Hi, Can I use Winsock to communicate between two process on the same computer but without network card??? THANKS. Dave wrote: > Hi, > Can I use Winsock to communicate between two process on the same computer > but without network card??? You don't need a network card to install a Microsoft Loopback adapter then you have a local NIC driver without a NIC. At that point you could simply use WinSock as documented and connect to localhost (better) or the assigned IP address on the Loopback adapter. I mention this method only because I suspect (but don't actually know) th...

sockets
I am using CSockets to design a simple chat server/client. The client is able to send messages to the server but fails to receive anything back. Here is the code for the client that I have put in a while loop: char echoBuffer[256]; // Buffer for echo string int recvMsgSize; // Size of received message while(true) { recvMsgSize = client.Receive(echoBuffer, 256, 0); } This causes the debugger to prompt for a Debug Assertion Failure then it gives me the option to shutdown the client. Why is this happening? Jessica Jessica Weiner wrote: > I am using CSockets to desi...

timeSetEvent and mfc socket
Hi all, I'm developing a dll that use a timer. i used timeSetEvent with a callback function, to send a message every time period using MFC Sockets. the callback function works well, but when i add the code that call the send function from it, i get a socket error. [Debug assertion failed socore.cpp line 566] and when i trace the code i found it in function CAsyncSocket::AssertValid(). i guess the source of assertion is that the callback function run in a isolated thread. but i do n't know how to solve it. Thanks. Eid Mahmoud eid mahmoud wrote: > Hi all, > I'm developing a...

Socket
How can I connect to smtp server? I tried to use CSocket: CSocket csk; csk.Create(); csk.Connect("server.com", 25); but there is an error during connecting to server.com (function Connect return false).... Thx for help me ArtuS "ArtuS" <artu_s@gazeta.plNOSPAM> wrote: >How can I connect to smtp server? >I tried to use CSocket: > >CSocket csk; >csk.Create(); >csk.Connect("server.com", 25); > >but there is an error during connecting to server.com (function Connect >return false).... Have you called GetLastError to find out wha...

UIThread and Socket ?!? (socket works fine but....)
hello i have written an littel application with a UIThread. In this thread i create a Socket, to receive any msg If the CAsyncSocket::OnReceive() fired, i read IN THE SOCKET-CLASS the incomming Data. After thi i call the Thread with PostThreadMessage() to process the Data When the Thread is working and a NEW data-msg is comming up, the Socket read it exactly !!. But the PostThreadMessage() call in my SockteClass is without reason. Also the Thread don´t get the Message ! How can i solve the problem thx in advanc p3p I'm not sure what you mean by the questions. What do you mean ...

Asynchronous Sockets Client
Hello, I'm trying to create an Asynchronous Client using the sample code from MSDN http://msdn.microsoft.com/en-us/library/bew39x2a.aspx My problem is that if the connection is refused, e.g. the server is not started. I have no way of knowing this! How do I return some kind of status from an async method? -- Some snippets.... In the main form MyClient vision = new MyClient(); vision.Connect(); // here I don't know if the connection is OK or not // Async socket client class class MyClient { Socket client; private static ManualResetEve...

Using Sockets
I am using the CAsyncSocket class and I want my write operations to block. How can I set this parameter? Use CSocket, which simulates a blocking socket and is derived from CAsyncSocket But be warned that there are some very polarized views out there as to whether CSocket is just fine or is instead the source of all evil. "Chris Baker" <ChrisBaker@discussions.microsoft.com> wrote in message news:2D52BD1A-17EA-436A-9B1C-DBE24B070205@microsoft.com... > I am using the CAsyncSocket class and I want my write operations to block. > > How can I set this parameter? > ...

Socket and multicast...
Hello, I am using VC++.Net and CAsyncSocket class. I am writing an application connected to 2 networks : The company network with DNS and network services A private local area network with no DNS ans servers but computers on some multicast groups I send a multicast request to the private local network but the frame is sent on the company network and not on the private network. What I tried : I set DONT_ROUTE and NO_BROADCAST on the opened socket of the local network, I tried to open on the same UDP port on the company network and shutdown the socket Thank you for help in advanc...

System.Net.Sockets.Socket question
I have an application that accepts an existing socket connection (passed to it by the server that actually accepted the incoming connection), and I'm trying to find a way to have the application NOT close the socket when it terminates. So basically what I've done is created a class that inherits from System.Net.Sockets.Socket. In the .NET source code I can see that the Dispose() method closes the socket if it is still open, so my first thought was to override the Dispose() method and leave out the socket closing portion of the code. This failed, so maybe my understanding o...

Robust Server using Sockets
Hi, I want to develop a server application which can handle 1 million clients. I plan to use asynchronous tcp/ip sockets to handle clients. The communication is not frequent and the data transfered is also less(Max 100kb packet) I have some ideas to implement it 1. To have a single server which will service all the 1 million clients. Server will listen to the connections on a particular port and once connected will keep the connection open forever. 2. To have a single server which will service all the 1 million clients. Server will send data in one port and receive data in other port. Onc...

Socket probem,GetLastError returns 10038 (Invalid socket handle)
hi all I am implementing SMTP protocol in MFC using Sockets. I am using the CSMTPConnection v1.36 code provided by PJ Naughter at http://www.codeproject.com/internet/csmtpconn.asp The problem is while sending mails sometimes mails are not sent. 1)I am connecting ,sending mail and then disconnecting. The above step is repeated 6 times hence 6 mails should be sent.Sometimes only 4 or 5 mails are sent.But sometimes all 6 mails are sent!!! The problem seems to be while Sending the MAIL command in SendMessage()function The return value of GetLastError() was 10038 i.e Socket operation on ...

socket #2
I've made a class CMYSocket that derives from CAsyncSocket. Does it matter when I call CAsyncSocket(nErrorCode) ? Before or after calling receive(...) ? Do I need to add my own check of the nErrorCode variable and stop the object if there the variable is not equal zero ? On a few occasions my OnReceive method has been triggered when no data has acctually been sent. The GetLastError() method returned error code 10054 (WSAECONNRESET). quote: ---------------------------------------------------------------------------- -- Connection reset by peer. An existing connection was forcib...

Sockets
Hey everyone. I'm writting a simple chat-like client that uses straight CSockets (with Cfiles and CArchives of course) and I've run into a bit of a problem. Initialization and connection run great, the problem is how do I know if there is data waiting at the socket to be read? Basically if I send across a message to my server, then call CArchive.readstring() it works great, however if they are no messages currently waiting, then the program simply hits an infinite loop :( I know that the method is looping in the Read method, but I have no idea why.... Okay, so is there someway of t...

Protocols used by socket (Datagram)
Hi, I know that protocol used by CAsyncSocket (or CSocket) in Datagram configuration is UDP. I'd like to known the other protocols used during the communication and principally the initialization or "connection" phase. Any information making the relation between CAsyncSocket connection steps and the protocol used would be great! Something like : CAsyncSocket::Create() : ::Master:: ::Slave:: 1- ARP[Data ...] 2- ARP[Data ...] 3- UDP[Data ...] 4- UDP[Data ...] CAsyncSocket::Write(): ::Master:: ...

Sockets/Networks
Are there any MSDN articles similar in style to "Serial Communications in Win32" but aimed at sockets/network communications? Thanks in Advance, Charles Charles wrote: > Are there any MSDN articles similar in style to "Serial Communications in > Win32" but aimed at sockets/network communications? > > Thanks in Advance, > Charles > http://tangentsoft.net/wskfaq/ -- Scott McPhillips [VC++ MVP] ...

socket.close()
I need to close a socket and use it immedeiatly again with another local port. Is it enough to do socket.close() in order to release all resources, or must I do anything more(like a delete or similar) before I use the "new" operator? Regards Ronny Ronny wrote: > I need to close a socket and use it immedeiatly again with another local > port. You can't. > Is it enough to do socket.close() in order to release all resources, Yes, Close() is equivalent to Dispose(). But after you close the socket, it can't be reused. You have to create a new ins...

Sockets in MFC
The project I am working on was set up to support sockets in the MFC setup. I have a socket class that sets up the server side of the socket relationship. On a simple console based app my socket code works. It is essentially a copy paste from the MSDN socket example material. So I took my basic console functionality and am trying to get it to function through an MFC dialog. When I try to start the server object so it waits for a client now, the program locks. I ran the debugger to find the place where it stops, and compared all the variable states on the dialog vs the old conso...

Sockets and Notification
Hi everyone, I have a client/server application talking to each other using streaming sockets. I have a class that is the communication class, which owns the socket, sends, and receives messages. Anyway, Lets say I want to call a method in the communication class, tell it to send a message to the server and wait for a response. So the client will send a message to the server. Wait for a message from the server. If it doesn't receive the reply back within 2 or 3 seconds, it raises an error. I am having a bit of problem designing the wait part. Any ideas would be appreciated Thanks Ali...

Completely GENERAL specifications for socket notification
I've looked in the MSDN to find out the exact behavior of the On*() notification functions for CAsyncSocket, but there are many important things that are not very well described. Can anyone point me to something that gives a clear and completely general description of how these notifications work? This is the data I've been able to piece together so far: OnReceive "Notifies this socket that there is data in the buffer for it to retrieve by calling Receive." Does this mean that every chance it gets it checks to see in there's something to be received then calls OnReceive?...

Sockets, Am I crazy??
Win2k SP3 Visc Studio 6.0 SP5 Snippet of MFC code, UDP Socket I create a socket / class if (!m_pSocket->Create(9600,SOCK_DGRAM)) then do the OnReceive(int nErrorCode) method to call receivefrom For some reason, this event (OnReceive) fires all the time and does not appear to be just 'bound' to the 9600 port, which of course brings my app to a crawl because I am continually trying to parse every packet hitting the card, not just ones directed on port 9600. Any ideas how to bind the socket to receive only on port 9600, I thought Create automatically bound the socket to the port...

Socket TimeOut
Hi; Can I change the timeout of recieving , sending...? I tried to use SetSockOpt but nothing happened Monther "monther" <monther80@hotmail.com> wrote in message news:OrOsW8wXDHA.3444@tk2msftngp13.phx.gbl... > Hi; > Can I change the timeout of recieving , sending...? > I tried to use SetSockOpt but nothing happened > Monther > > > See documentation for struct linger in MSDN. Hi; I read all documentation about this problem,The problem actually is seriouse in recieving form someone, if there is no recieved data , the programm will stop at recieveform...

Clearing the data in the socket..
Hai.. Can anyone tell me how to clear the data in the socket after receiving it?? Urgent... Thanx in advance.. "Wallace" <princevictor.moses@gmail.com> wrote in message news:1140602678.861429.68730@o13g2000cwo.googlegroups.com... > Hai.. > > Can anyone tell me how to clear the data in the socket after > receiving it?? //... static const unsigned nRead = (10*4096); BYTE pTmp[nRead]; memset( pTmp, 0, nRead ); int nActual = Receive( pTmp, nRead ); //... I am not sure I understand, calling Receive(...) will remove the data. if the data is greater than (10...

Socket Status
Hi guys, I've been trying to get the status of my socket but I can't seem to succeed. Basically I have a thread listening to my socket and processing packets, however when the socket on the other end disconnect I never know about except I keep getting packet of size 0 bytes. I tried setsockopt() to set the Socket for SO_KEEPALIVE and used getsockopt() when data comes in but no luck and I think I read it somewhere that in Win32 you only get this option after 2 hours timeout. I tried select() and that did not work either. So I'm at a dead end with no way to find out the status o...

MFC Socket listener
Hello everyone, got a quick question here that hopefully someone can help me with. Basically I am having trouble with my Client application which uses MFC CSockets. I currently can only listen to one port (say 4000), and I need to be able to listen to multiple ports. Originally I just set up two listeners (one listening on 4000 another on 4200) but the problem with this is that I need to know which Listener is flagged when I call the OnAccept() method (actually this method is calling another function OnProcessPendingAccept() at which point I need to know which listener got tagged). Basical...

SQLConfigDataSource kills socket
I am hoping someone out there can help. I have a program that communicates with a server program using TCP via CAsyncSocket. I modified this program to do database queries using ODBC. What I would like to do is to programmatically set the data source name using SQLConfigDataSource(). However, everytime I call it the socket connection gets disconnected. I wrote a simple stripped down program (not using CAsyncSocket) that reproduces the problem: CSocket s; BOOL result = s.Create(); ASSERT(result); result = s.Connect("localhost", 4000); ASSERT(result); result = SQLConfigDataS...