Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I built up my server for my app which is running on Android. And i will hear some opinions from you if the setup is acceptable or complete wrong. So the architecture works like request-respone.
The user will log-in, every app start and will get a new session id.
The client and the server does not have a keep-alive connection. The connection is getting cut every request. For every command on the server, the session id need to get checked first, and then the command or request can be calculated. After everything is done the connection will cut. I was thinking about something that the connection will be held to the server and this gives me the possibility to send from the server some data directly to the client. This could have many usages. But on the other hand its not thread "able". Because i will have to synchronize the socket access and share 1 socket object between all classes and activity, this isnt in my opinion a good way. But am still wondering how other apps or online games could sent data or messages directly to the client. This means that a connection is held. I think that they doing it seperated in a service or something like this. This a new problem im facing. I could use firebase cloud messaging, but this is very slow when more as 100 threads are running on the server. A better solution where, to code a second server program, which is running seprated from the main server and keeps a connection to the client. This would be my solution.
What i just want to know if my architecture is good to go or its a bad idea.
In my opinion, opening and closing the connection is a good practice, because the connection is relatively an expensive resource.
So, I would say that yes, you're good to go with the architecture that you currently have implemented.
Open connection
Execute operation
Close connection
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
for a project for school i need to write a chat program.
what i have so far:
database users(Username, Password, IPAddress, onlineStatus)
loginform
mainChatForm
registerForm
im able to connect to the database, register users, check online users,...
but now coms the tricky part and here is where im lost. I now need to write the chatprogram itself. i found some tutorials on the internet but they all use server-client. I dont what to do this i want to make it p2p.
i understand i still need to use the serversocket-socket thing. thats why i keep the users IP in the database.
i need some guidance on how to start to build the chat part of my project.
if someone could help me that would be great.
Thanks
DenTilloZie
If I understand correctly, you will need a central server that holds all of the login information, status of users, and their IP addresses. Users can register their IP address at login time.
In order to initiate a chat with somebody, a user will browse the list of online users on the central server, fetch the IP address of the person they want to communicate with, and then make a direct P2P connection to them.
Everything you need to know about actually implementing this is in the API or stackoverflow. If you have more specific questions about implementation you should probably make another more specific question.
If i'm not wrong, you can make two threads. One holds a server socket and the other a client socket. The server thread will be listening for new messages from the other peer, while the user thread, can be used to send new messages.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have gone through resources on building chat application using socket programming in java. In every implementation people try to make a server which runs in infinite loop, accepting the connections from client and creating a separate thread for handling the chat.
I want to make a chat application in which a new dialog/chat window pops when someone wants to chat with me (on client side). But the catch is that i have only one socket through which i am connected to server. all the messages has to be sent through this streams, currently i am thinking of some adhoc approaches for directing output to different client windows but i am sure that there must be some elegant way to do this.
If you want to use a single socket connection per client, then all communication should be multiplex over that connection, which means that you need to develop a protocol on top of socket streams between your server and a client. A protocol is a set of rules. For example, clients may issue commands and server respond to them, like one command, one response. The commands and responses need to be marked and separated somehow from each other, perhaps you want to add an identifier and a length of a message and then refer to that message.
Various systems use different protocols.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a small client server app , that involves the client pinging the server on a set of ports ( 90,6100,6200).
I use inet.isReachable(30000) to monitor whether the server responds to the client or not.
In the end , I want to make sure that when I re-run my application , any existing connections to these ports are removed.
How do I go about doing so ?
And is there a way to automatically close a connection when I am using InetAddress, which does not seem to have any method to terminate an existing connection to an address ?
"I want to make sure that when I re-run my application , any existing connections to these ports are removed."
The connections cannot stay open when your application exits - there is no process for them to be connected to. So in that sense, you don't have to worry about closing them on next launch.
"And is there a way to automatically close a connection when I am using InetAddress, which does not seem to have any method to terminate an existing connection to an address ?"
Yes - the connection will close when isReachable is responded to, or when the timeout expires (or some other error occurs). Just creating an InetAddress does not create a connection - it automatically creates and destroys them as needed to do its functionality. Connections are generally just short lived things that don't stay open unless you make them.
The JVM will close the connection after the server responds to isReachable or the timeout expires, whichever comes first.
The answer to your vague question is that as connections don't survive the process they are created by, and as INetAddress"isReachable() doesn't leak connections, there is no actual problem here to solve.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
My problem is the following, I have two servers from which I have to transfer files, in both directions. The transfer is triggered by a file creation event (on respective sides). The problem is one server has a public IP, the other one doesn't.
I have implemented a socket client that sends a file over a socket, and a socket server which receives and saves it. (Working part)
My questions are : How to keep the socket 'alive' and send some data to the client after a file-system event occured on the server-side ? (Can the server call the client without knowing it's public IP ?)
Can I achieve this with socket technology or should I go for something else like RMI ?
The problem I see is not really an implementation issue. The problem is that you want to keep the client without a fixed address. If you had a fixed IP, I suppose there would be no problem. Right? As you probably understand there is no easy way for a computer to be called without having an address.
An option would be to use an middle solution, wrapping your non-fixed IP with a DNS able to refresh. You could use a service like dyndns to get a domain name which will actually redirect each packet to the real IP. Your router would have to be configured accordingly in order to refresh the IP to the dyndns servers each time it changes.
Another option, would be to use the websockets paradigm which now is part of HTML5. This way, the server would be able to push content to the client whenever he wanted it.
All of the above solutions depend heavily on your detailed scenario and I cannot by anyway guarantee that what I suggest is the best solution. Actually, I would strongly suggest to get a fixed IP which is a lot costless and cleaner solution than the ones I describe.
Hope I helped!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to java programming and i want to develop a server that can store the details of the clients to know which client has already made connection to it.
Please give me some good links and examples where i can learn how to design a server.
start with ServerSocket, Socket, and InetAddress. Those should let you get started and figure out all clients connected to your server, then compare the IP addresses to see if there's a duplicate. You should probably get started, then post some questions on that.
While MartyE's answer about server sockets is entirely viable in the right context, a much more common model in this day and age is to develop servers with web technologies. I'd recommend starting by looking at Java Servlets, and then perhaps frameworks like Restlet/Jersey if you don't need a web interface.
These would let you develop an HTTP + XML or HTTP + JSON server relatively painlessly.
Developing a server directly with sockets would give you more control and if you do a very good job of it, it would probably be more performant than a web-based solution, but it's more work and there are more ways you can trip yourself up on concurrent programming issues and socket handling. It's not what I'd recommend if you're new to programming.