recvfrom Problem

Started by
4 comments, last by Galileo430 23 years, 3 months ago
Ok here goes. I don''t know much about Winsock so feel free to laugh and then correct me. I am makeing a console app to be a server for my game. I have started up Winsock with WSAStartup. Bound the socket with the following settings and TCP/IP protocol. socConnection[0] = socket( AF_INET, SOCK_STREAM, 0); adrServer[0].sin_family = AF_INET; adrServer[0].sin_port = htons (5555); adrServer[0].sin_addr.s_addr = htonl(INADDR_ANY); Then I started a tread to recvfrom with OOB (Setting the OOB flag on TCP/IP makes it UDP right??) However instead of the function waiting for data. It returns socket error. Last socket error being 10057? Anyone have any idea what I did wrong?
------------------------------------------------------------I wrote the best video game ever, then I woke up...
Advertisement
You shouldn't use recvfrom()/sendto() on a stream socket, just datagram sockets. Notice that it requires the address of the remote socket, so that you can recv/send a packet with anyone on the network.

A stream socket is like a telephone: one person picks up the phone and dials out to the other person, who picks up his receiver and they have a 1-1 connection.

To use streams, you need to decide if you are going to dial out to someone specific, or wait for their phone call. To dial out, you just create the socket and connect(). To wait for a call, you bind()/listen()/accept(). Once the connection has been established, you use recv()/send() to exchange data -- neither of which require an address because that's a given.

To create a datagram endpoint, change SOCK_STREAM to SOCK_DGRAM and you can call recvfrom()/sendto() freely.

Edited by - fprefect on February 3, 2001 6:44:28 AM
Matt Slot / Bitwise Operator / Ambrosia Software, Inc.
Thanks alot.

Ok, got it.. Now hear comes the major question..

I want to use a almost pure UDP server (TCP/IP need not apply, although it may be used for reliable payloads in the future).

What I was planning is someone gets the IP from a master server.
Sends a simple packet UDP to the 5555 port of the server. It sends back data. Then if he decides to connect it calls back up the 5555 port which transfers him to another port that he connects to (aka. Blocks). Then sends UDP over that connection.

First, Can I do what I am thinking of?
Second, Anyone know a tutorial on how to use UDP (I checked www.google.com)?

------------------------------------------------------------I wrote the best video game ever, then I woke up...
It''s even simpler: the master server will send you the address of the game along with it''s description, and you can just start communicating with the desired host directly. There is no "connection" to transfer with UDP, you just send packets at any address you like.

As for sample code, you can take a look at this article:

http://www.flipcode.com/network/

It''s a pretty decent start.
Matt Slot / Bitwise Operator / Ambrosia Software, Inc.
TCP/IP is the name of a set of protocols used for the GAN (global area network ie the Internet)

TCP is a protocol that is part of the TCP/IP suite
UDP is another protocol that is part of the TCP/IP suite
ICMP is yet another

You can have mutliple TCP conenctions and have multiple UDP transmissions concurrently, so long as they all use different ports.

I think UDP & TCP have seperate ''port pools'', but I''m not certain. ie I think you can receive TCP & UDP packets on port 1024 at the same time... but you might not be able to. Know how that works Ford *blink, blink*?

PS
// MessageId: WSAENOTCONN
//
// MessageText:
//
// A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.
//
#define WSAENOTCONN 10057L



Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Here comes the other Prefect

Yes, TCP/UDP have seperate port pools. It''s all very logical when you look at how TCP and UDP are just protocols using raw IP.
And you can have multiple TCP/UDP connections running on the _same_ port. You just can''t have two different connections between the same host/port-pairs.
Oh, and you shouldn''t use ports below 1024 for games, as on proper OSs (i.e. *nix and the like) those ports are reserved for the administrator/super user.

cu,
Prefect

---
Sanity is the trademark of a weak mind.
Widelands - laid back, free software strategy

This topic is closed to new replies.

Advertisement