I made a UDP server and now I want the server to be TCP, I don't like losing packets.
Will it be hard to reverse it to TCP or just a few things?
What things should I change to make it TCP?
Can I still use all my packet classes?
I know I have to change the DatagramSocket but don't know to what.
You should read about TCP sockets in Java, as Parker suggested.
If you want advanced functionalities for your server, such as session management, you can use a socket framework like Apache MINA or Netty.
Related
I'm trying to develop a game with using java server and game maker studio client. I've written some basic connection code and it works fine in my local network. But when I host the server and try to connect my client gets the ID assigned by server which sent through TCP but it does not receive any commands sent by UDP protocol. How server can't establish connection over UDP while it can over TCP. I heard I need UDP hole punching. But once I'v written the server with game makers studio networking functions and UDP was able to establish connection with same configurations on the server. So they are using hole punching in their built-in functions ? If I need to implement hole punching instead should I use TCP only or I should learn hole punching for any cost ?
There are quite a few questions here so I'll try to break them apart piece by piece. First of all...
Does NAT block UDP packets coming from a server connected using TCP?
Yes. Just because the NAT has established a TCP connection between two machines does not mean it will automatically forward UDP packets as well.
How server can't establish connection over UDP while it can over TCP?
TCP is a connection oriented protocol while UDP is a connectionless protocol, so there is technically no such thing as a "UDP connection". When two computers communicate via UDP they simply exchange UDP packets back and forth and the "connection" is implied (or managed at a higher level), rather than spelled out by the UDP protocol itself.
I heard I need UDP hole punching... If I need to implement hole punching instead should I use TCP only or I should learn hole punching for any cost ?
UDP hole punching is one possibility but it is complicated and not 100% guaranteed that all NATs will be compatible. Instead, you could build your game using just TCP, then once it is working and deployed, experiment with more advanced communication channels like UDP hole punching or WebRTC Data Channels.
I am learning about UDP client server where the communication is one way from Server to Client. Server will send UDP packets to Client.
I want to to make it dual communication where Server and Client can send UDP packets to each other.
Do I need to make another Datagram socket for Client Server to make it dual communication or can I use my existing Datagram socket to do the job. If I dont need another Datagram Socket , I would appreciate if you can show me how it is done
I would appreciate if you can point me to relevant resources on dual commuication for Client Server
Yes, you can use the existing DatagramSocket.
Take a look at example in this link:
http://phoenix.goucher.edu/~kelliher/s2011/cs325/feb25.html
Its upon you how to use the send and receive methods of a socket depending on your needs.
I'm developing a Client application which talks to a Server using WebSockets. The Client is in C++ and the Server is in Java.
Can anyone suggest me any library which I can use on both Client side and Server side for communication using web sockets.
I never had experience with WebSocket, but try library cURL (libcurl). It was easiest for me to write clients for HTTP and FTP, using it. It have to help (but curl is useful just for clients, not for server).
If you are talking about sockets, normal sockets that connect on a port and wait for a connection on the server side and that connect to a given address on the client side, then I would recommend the boost asio socket on the c++ side and the standard java socket on the java side.
Just remind yourself of making sure that you transmit the datatype you expect.
Another cool implementation for both, java and c++, is ZeroMQ. I would recommend to take a look at it because it is easy to use and has implemented some really cool communication patterns.
My service already uses Websockets to communicate with an HTML5 in-browser client. The client is served by the same server from a normal http request.
Now I would like to offer the same service/app but out of the browser, and I would like to offer it over TCP sockets.
The RPCs/action object I am using are going to be the same, the serialization is going to be the same, the logic is the same. I just want to use TCP socket instead of WebSocket.
I would like to keep the code together under the same "project folder", starting all at once when I deploy the playframework server (basically on start I want to start listening to WebSockets, TCP sockets and http requests), and have everything in the same package on deploy.
I know that:
It is not necessary, since WebSocket can be used in not-in-browser apps, but consider this an exercise or a curiosity question.
playframework is built on top of netty, and I used netty before to do some TCP services (nothing big and nothing prod ready though ... so not an expert). So they should work together right?
What I was thinking to do:
Have an akka actor listen for new socket connections.
Wrap the connections (WS or TCP sockets) into a ClientConnectionManager instance
Pass it to the actors that takes care of the connections/rpc logic.
Other leads I considered: Reimplementing the playframework Controller class.
Or is there an already implemented solution for this?
Essentially Im trying to get many many java clients connect to a socket on my ColdFusion server (Using the Socket Gateway). However before i even start to code this, Im a little confused about sockets and their performance. First of all, are sockets meant for many(1000+) clients connecting to one socket (say port 2202) on one server? How is the performance if all there waiting for is basically a ping, or something such that when these clients receive this "ping" they can go get some new data.
Thanks,
Faisal Abid
Socket is identified by following tuple,
Source IP
Source Port
Dest IP
Dest Port
Protocol (TCP or UDP)
Even 1000 clients all connect to the same port (dest port), each will get its own socket. So you will have 1000 sockets open.
It's going to be tough to maintain 1000 sockets with blocking I/O, which usually means 1000 threads. You need to use NIO. We have a server written with Mina, which can handle 2000 connections at peak.
First of all, are sockets meant for
many(1000+) clients connecting to one
socket (say port 2202) on one server
Yes, your server will open a socket on port 2202, and 1000 client will connect to it.
Server open server socket, and client will open client socket, it different.
How is the performance if all there
waiting for is basically a ping, or
something such that when these clients
receive this "ping" they can go get
some new data
On server, you use getInputStream function to get data from client, and getOutputStream function to send data to Client.
Notice: you should use Thread to process each request of client
There is nothing wrong with many socket clients that use blocking I/O (you may have a look at this article for more information about that). However, there are another approaches that can better fit your needs here:
nio;
multicasting;
Hi connecting with 1000 clients at the same time using simple java socket programming is not the perfect way. The probelm is that in fedora linux default maximum no of file can be open is 1024 and in windows it is 2048 or something like that. so in your server side you will find more than 1000 files open and after that if client will go on increaing then you will find too many files opnened error. so the better way is to use non blocking socket programming (I mean to say to use SocketChannel) Using socket channel at the same time as per i have check we can connect with 20,000 clients without any problem.
So better use nio. there is a very good book from Oreilly publication for java nio.
I have used java nio (socket channel)
Thanks
Sunil Kumar Sahoo