Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Simple Socket Server For Local Multiplayer Game
Started by hmclark2014 Nov 12 2018 04:22 PM

27 replies to this topic
socket network games

Best Answer roaminggamer , 13 November 2018 - 09:55 AM

I dug up an old product of mine and stripped out the Client-Server Code.

 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/11/peer2Peer.zip

 

It is not perfect, but it still seems to work.

 

This example uses the client/server modules to:

 

1. Create an app w/ two choices. 

  • A. Be the server 
  • B. Be the Client.

 

2. After you select server (can only be one) or client (1+ is OK), the app allows you to send and receive messages.

 

 

Notes:

  1. The code in main.lua uses SSK for the buttons and to set up the message event listeners.
  2. The two modules each have a tiny summary of functions at the top.
  3. From any client, you can send messages to the server, a specific client, or all clients.
  4. From the server you can send messages to a single client or all of them.
  5. While this code seems to be functional, you need to verify it on all targets and between them.
  6. You need to set up ATS in build.settings if you want to test on iOS.  I forgot to add this in the example.
  7. If you find this fails in some cases, please post back with specific details about the failure and test device(s) so I know it failed.
  8. I may add this directly to SSK later, but right now you get an early sneakpeek.
  9. I used 'localhost', but you may need to use the machine IP instead.  The code to set that is already in the example, just commented out.

 

 

PS - I have only tested this on Window 10 running three instances of the simulator (2018.3363).

 

PPS - The name of the example 'peer2Peer' is a misnomer.  Originally I tried to provide a pure Peer-to-Peer (serverless) example, but I could not.  So, I just used the same base for a client-server solution instead.

[TOPIC CONTROLS]
Page 1 of 2 1 2
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Hi, Sorry if this is the wrong place to post this but I’m currently trying to implement a multiplayer feature into my game however I haven’t gotten very far. My game runs on iOS, Android and soon to be Apple TV so I need somthing that will work on all of those platforms. Making a simple socket server seems to be the way to go but I have no idea what I’m doing and after a week of trying to find info I’m still clueless. Could anyone send me a simple socket server where the server can talk to the client and vice versa or point me in the right direction? I’d like somthing where I can just call a function and tell the function to send a string to the other device so that I can send current scores or if a player is dead or alive between devices. Thanks

[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

I'd google for:

  • Lua Socket
  • Lua TCP
  • Lua game server
  • Lua server
  • ... you get the idea.

I'd also search over at github.com

 

Lastly, I have a modified copy of AutoLan, but I can't tell you how well this will work for you as I haven't used it for a while and things in the land of security have changed for Android, iOS, and Apple TV:

 

https://github.com/roaminggamer/AutoLan-IPv6

 

also found this:

 

https://forums.coronalabs.com/topic/26339-p2p-udp-tutorial-example/



[TOPIC: post.html]
#3

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Thank you, this should help me out a little

[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

I may have something to post back in a few day.



[TOPIC: post.html]
#5

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

So I've spent the best part of today using the information you told me to look up ways to do this. I've found many things like app wrap that allow you to use their service to achieve multiplayer however something like this inst what I'm looking for. Id like to have it so that a player can host a game from their device and another can join and it would work by sending strings to and from the devices. I found the original AutoLAN and it seems to be exactly what I'm looking for but it is old and no longer works and the newer IPv6 version is also broken due to being outdated. Making my own simple socket server seems to be the best option for cross-platform local multiplayer. I looked at the link you sent (https://forums.coronalabs.com/topic/26339-p2p-udp-tutorial-example/) and this would work for me however it only sends things from the client to the server and you cant send things from the server to the client and that wouldn't work for me. Is it possible for you to modify it to send stuff both ways? my attempts at this just make corona freeze up and crash.



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

Hi.  I have a different solution.  I'll try to post it in the next few days.  I'm crushed right now.

 

Tip: When posting in the forums, if you avoid wall-of-text (WOT) posts and make your posts easily accessible/legible you'll get more and better responses. 

 

I don't generally read WOTs, but since I was already in this thread I browsed your post briefly to respond.


  • Michael Flad likes this

[TOPIC: post.html]
#7

Michael Flad

[GLOBAL: userInfoPane.html]
Michael Flad
  • Contributor

  • 225 posts
  • Corona SDK

Hi.  I have a different solution.  I'll try to post it in the next few days.  I'm crushed right now.

 

Looking forward to what you're going to post ... I still have an old prototype on my drive I'd like to turn into a real game in the future and it was using AutoLan (can't remember the version)



[TOPIC: post.html]
#8

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

  Best Answer

I dug up an old product of mine and stripped out the Client-Server Code.

 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/11/peer2Peer.zip

 

It is not perfect, but it still seems to work.

 

This example uses the client/server modules to:

 

1. Create an app w/ two choices. 

  • A. Be the server 
  • B. Be the Client.

 

2. After you select server (can only be one) or client (1+ is OK), the app allows you to send and receive messages.

 

 

Notes:

  1. The code in main.lua uses SSK for the buttons and to set up the message event listeners.
  2. The two modules each have a tiny summary of functions at the top.
  3. From any client, you can send messages to the server, a specific client, or all clients.
  4. From the server you can send messages to a single client or all of them.
  5. While this code seems to be functional, you need to verify it on all targets and between them.
  6. You need to set up ATS in build.settings if you want to test on iOS.  I forgot to add this in the example.
  7. If you find this fails in some cases, please post back with specific details about the failure and test device(s) so I know it failed.
  8. I may add this directly to SSK later, but right now you get an early sneakpeek.
  9. I used 'localhost', but you may need to use the machine IP instead.  The code to set that is already in the example, just commented out.

 

 

PS - I have only tested this on Window 10 running three instances of the simulator (2018.3363).

 

PPS - The name of the example 'peer2Peer' is a misnomer.  Originally I tried to provide a pure Peer-to-Peer (serverless) example, but I could not.  So, I just used the same base for a client-server solution instead.


Edited by roaminggamer, 13 November 2018 - 10:50 AM.

  • Michael Flad likes this

[TOPIC: post.html]
#9

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Thanks a lot this looks promising, I’ll mess around with it when I have some time today. I’ll let you know how it goes. Also I’ll try to avoid writing wall of text posts, this is only my 2nd fourm post so I’m kinda a noob at writing posts.

[TOPIC: post.html]
#10

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,013 posts
  • Corona SDK

Use gamesparks and don't reinvent the wheel



[TOPIC: post.html]
#11

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

I tried running the server in the sim and it works great but as soon as I try it on a actual android device it gives me a runtime error at line 103 in client.lua

[TOPIC: post.html]
#12

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Good thing is that it’s exactly what I’m looking for

[TOPIC: post.html]
#13

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

What steps did you take to debug this?

 

Did you you update build.settings to enable internet for Android?  My sample was only setup for desktop.



[TOPIC: post.html]
#14

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

Try downloading it again and give it another try.   I updated build.settings.

 

It does not work for me, but I have my WiFi and local network heavily locked down.



[TOPIC: post.html]
#15

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Ok will do, if it still persists I’ll see if I can fix it if not I’ll post the error here

[TOPIC: post.html]
#16

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

I was just wondering how does this work? when it is in the simulator it will connect to another simulator just fine but as soon as I build the app and test a simulator to device scenario it wont receive.

 

So I went to debug and I see that when the server is created the address is set to "*" and the port is set to 0xc001 but the clients address is localhost and the port is something different as well. Do both addresses and ports have to match? If so how do I do this.

 

I'm about to do some more debugging which will include running the app on real devices and un-commenting the code you mentioned in your post that uses the machine ip instead of localhost.

 

But from what I've seen so far this is exactly what I was looking for.



[TOPIC: post.html]
#17

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Also you where right about the build.settings, I completely forgot to enable the internet permissions that’s why I was getting the runtime error

[TOPIC: post.html]
#18

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

I'm afraid I don't have time to give a detailed explaination on how it works.  You'll want to find some external docs on sockets.* and read more about that.

 

However, to answer some of your questions:

 

1. The addresses do not need to match.  However, the devices all need to be on the same sub-net. 

 

For example, if your PC is at 192.168.??.?? then your other devices all need to be at 192.168.???.??? too.

 

For most folks this is not a problem since they have only one wifi network and it is usually on the same sub-net as their wired network.

 

 

2. The ports will all be different.  Each device that connects will have its own port number.



[TOPIC: post.html]
#19

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Hi, I'm working on it now and I've tried changing the address of both the server and the client to the ip of the machine that its running on and it works fine in the simulator however it doesn't work while using the simulator and a actual device.

 

I'm gonna try it on actual devices to see if its the simulator that's the problem.



[TOPIC: post.html]
#20

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

So I've tried it on real devices and I've messed around with the ports and addresses to no avail. What do you recommend I do to get it to work?



[TOPIC: post.html]
#21

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

I have no further recommendations aside from learning more about socket and researching online.



[TOPIC: post.html]
#22

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Ok, funny I just got it working 5 minutes before you sent that. I cant thank you enough without your help I wouldn't have been able to do this.



[TOPIC: post.html]
#23

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

Be sure to share the modified working files here.

 

Sharing a project that uses them and demos them working would  be the best.

 

Just attach it to the post as a zip file using the 'more reply options' button below.


  • Michael Flad likes this

[TOPIC: post.html]
#24

hmclark2014

[GLOBAL: userInfoPane.html]
hmclark2014
  • Observer

  • 18 posts
  • Corona SDK

Ok I'll be sure to send it once it fully implemented in my game and it works its best



[TOPIC: post.html]
#25

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

OK, but in the interim can you please describe the change(s) that allowed you to get it working?


  • Michael Flad likes this


[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]

Also tagged with one or more of these keywords: socket, network, games