Jump to content
  • Advertisement
Sign in to follow this  
linek98

SDL_net receiving packet from server

This topic is 3254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just as title says. I can send packets from client to server and operate on them but I have no idea how can I send packets from server to client. I'm using TCP and as said in title SDL_net library. I appreciate any help. PS: It's about how to send packets from server to client and operate on them via client. I'm using Windows XP.

Share this post


Link to post
Share on other sites
Advertisement
On the server side you should be creating a socket for each connection

SOCKET newConnection;
newConnection = accept( listeningSocket , connectionAddr , addrlen );



After the accept call you can now do things such as

send( newConnection , buffer , size );
recv( newConnection , buffer , size );



so on the server you would just use the socket created by accept. and perform your send/recv operations on it. now if you do not store the connection in some kind of container i.e <vector> or <list> then after the iteration of that loop on the server your connection will be lost and you cannot perform operations on that socket any more.

I know this is pure sockets and not using SDL_net but it should be very similar. also there is a link in my sig to Beej's guide you should check that out it could possibly help you.

Hope that helps have a good day

Share this post


Link to post
Share on other sites
Thanks for reply but u didn't understand my problem.

I can send packets to server and I know how to handle them like:
I'm sending packets to server and it says it received a packet from the client. Everything works great. The thing is I wanted server to send informations about map to client so client could show map around player. I just have no idea how I can do what u said but the one receiving message is client.

Look, I have script which handle the packets sent from client to server. I just dunno how to handle packets sent from server to client.

Means:
Client->Send Packet->Server->Handle Packet->Send Packet to client->Client-> Here I dunno how to receive packet from server.

When I'm trying to receive packet using socket to server it just doesn't work. When I'm trying to receive packet using socket which is not server it works great.
Seems like it doesn't want to receive packets from server but server receive the packets from client easly. Where is the problem then?

Just explained it on several possible ways dunno how to say it more clearly ;p

Share this post


Link to post
Share on other sites
Quote:
Original post by linek98
Just explained it on several possible ways dunno how to say it more clearly ;p
Post the relevant code (enclosed in [source][/source] tags). You really haven't explained your problem in a way that anyone could understand.

Share this post


Link to post
Share on other sites
Quote:
Original post by linek98
Ok, I'll just ask. How to send packet (in server application) and receive it (in client application)?
Exactly the same way you send in the client and receive in the server. You have a socket on the server for each client, right? Just send() to the socket, and recv() on the client end.

Share this post


Link to post
Share on other sites
And it's same in SDL_net? Damn then I have to check why it doesn't work for me. Thanks for replies.

PS:
Can't really paste the code here bc it's whole game (too long) just wanted to make it multiplayer.

Share this post


Link to post
Share on other sites
Quote:
Original post by linek98
Thanks for reply but u didn't understand my problem.

I can send packets to server and I know how to handle them like:
I'm sending packets to server and it says it received a packet from the client. Everything works great. The thing is I wanted server to send informations about map to client so client could show map around player. I just have no idea how I can do what u said but the one receiving message is client.

Look, I have script which handle the packets sent from client to server. I just dunno how to handle packets sent from server to client.

Means:
Client->Send Packet->Server->Handle Packet->Send Packet to client->Client-> Here I dunno how to receive packet from server.

When I'm trying to receive packet using socket to server it just doesn't work. When I'm trying to receive packet using socket which is not server it works great.
Seems like it doesn't want to receive packets from server but server receive the packets from client easly. Where is the problem then?

Just explained it on several possible ways dunno how to say it more clearly ;p


Iam sorry I was not more clear. I did understand your problem :) and I showed you how you would send on the server with the new socket that is created when you get a new connection.. the same socket you "can get info from the client" on is the same socket you use to send "information back to the client about the map"

Again I will suggest looking at the Beej's guide on my sig. It will help with the basics of understanding sockets. have a nice day :)

Share this post


Link to post
Share on other sites
Another piece of important information: The server always responds to the client -- the client always initiates the connection.

With TCP, the server calls bind()/listen()/accept(), and the client calls connect(). Once the server gets a client-connected socket with accept(), it can send data to the client through that socket, until such time as you close the connection by closing the sockets.

With UDP, the server sees the client address in the call to recvfrom(); it sends data back to the client by calling sendto() to the address it got that way. Because UDP is connection-less, you cannot assume that the client will be at that address "forever" -- if you have gotten no data for a period of time, say a minute, you can assume that the client has disconnected, and you can't send more data to the client until it re-connects sometime later, possibly from another address.

If you do it like this, then your application will generally work well through NAT routers (check the Forum FAQ for more details). In general, it is not advisable for servers to decide that they want to connect to some client they may previously know about -- outgoing connections from servers will not work well through NAT, and may also be signaled as possible bad traffic in certain hosting situations. In fact, it's generally a good idea to configure the firewall on the server to not allow outgoing connections, period; that way, if you're hacked, at least your server can't be used to DDoS someone else.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!