• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with server and client sockets.  RSS feed

 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am in a situation where I need to create a server socket at server side to listen while at the same time the client side should run client socket. I need a clue how to implement it. The situation is like running through the servlet a server socket should be created at the server side after that client socket should be create at client side. Both these should should communicate and the established socket at the server side should be saved in a connection array. Can please some one help me out of this?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start here: http://docs.oracle.com/javase/tutorial/networking/sockets/index.html. It includes a working client/server pair.
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Start here: http://docs.oracle.com/javase/tutorial/networking/sockets/index.html. It includes a working client/server pair.


I know this. You know I am working on game project my education project. I need to create game room where players will be joined after they login. Players who successfully login will join this room and should stay in this room using socket programming. Now for this there must need to be a server socket and client socket that must need to be created. Once player enters this room they should receive and transmit data using these sockets. Now the problem is if I construct the server socket to listen to the client socket. Then program will stuck there and waiting for request to come in. It would stay there for infinite time unless there is a client side socket is started. And in order to start the client side socket the control should be transfered to client side (javascript) from servlet. Now I am not getting any idea how to fix this issue.??
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take a step back and look at the design. You talk of clients using sockets, but also of clients using JavaScript - so the client is a web browser? If so, what kind of socket would they be using - WebSockets?

If this is to be a web app, I would advise to use HTTP between client and server. That way you don't need ServerSockets at all, and could use the servlet API.

Or use WebSockets if it's supported by the browsers you need to run this on. In that case you need Tomcat 7 or 8 running on Java 7 on the server side.
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look I have used request response model to sign in. But the problem is if I am going to use same request response model in game room then it would require polling for communication medium. And polling is a wastage of resources. so I decided for game room to be in socket programming. What do you comment on it? How it should be implemented?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think anyone can meaningfully comment on it until we know what client you're talking about. Is it a web browser? Is it a Java desktop app? A mobile app? Something else?
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I don't think anyone can meaningfully comment on it until we know what client you're talking about. Is it a web browser? Is it a Java desktop app? A mobile app? Something else?


web browser
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
waqas imtiaz wrote:And polling is a wastage of resources. so I decided for game room to be in socket programming. What do you comment on it? How it should be implemented?


Your communication is going to take place over a network, so sockets are going to be used regardless of what you do. So deciding to use sockets is a non-decision. Besides, you made that decision based on premature optimization so it really wasn't a very good decision.

But now I understand your client is going to be a web browser. That means that your client is going to be written in Javascript, right? Javascript can't open its own sockets, it relies on the browser to do that. Unless you were planning to write your own web browser with some other features to support your client?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you'd either be using something like JQuery to make HTTP requests, or WebSockets. Either way, your server-side code would not create a ServetSocket, but rather use the standard API methods to handle requests.
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either I use JQuery to make http requests or web socket. What would be the pros and cons in each case? Also, which method is better and is easier to implement? Remember I am just a student of Java with this project not an expert. What do you recommend me to go with?
If some suggestions are given then please provide some details how it implement it. Thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend to make HTTP (AJAX) calls via a JavaScript library. jQuery is the most common one, but there are others. WebSockets are a newish approach that may not have all the kinks ironed out yet on either the client or the server side, and there isn't yet much documentation and tutorials for it out there.

If you have specific questions about that, the HTML/JavaScript forum would be the place to ask them.
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you know the all story. Please give me youtube links or any other reading stuff about ajax and how the server side would be implemented. So that I could do all this stuff. Thanks
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What it should be at the server side?
The servlet or ???

Please guide me on this.
Oh and yeh I got jquery tutorials on youtube.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!