I Am trying to code a java program with sockets in which i have 3 system(1 server and two clients) both the clients connect to the server at the same port.Its a file transfer program.
Now my requirement is that both the connection would remain active throughout and as soon as client1 writes to its socket connection on the server the server relays that data to the socket between server and client2.
How can i achieve this?
There are TONES of client/server chat examples out there for many different languages. All of them are fairly similar.
Try Google Java client/server chat tutorial. Here's one if you couldn't find an example.
http://inetjava.sourceforge.net/lectures/part1_sockets/InetJava-1.9-Chat-Client-Server-Example.html
Jinith: transferring a file is not that different from sending text over a socket. Do you have any specific requirements for how the clients know to get data? Are you familiar with blocking/nonblocking/asynch sockets? Comfortable using threads?
Related
I am studying communications in Java using RMI and in all the references that I find there is a client that makes calls to remote methods in a server.
That is, all communications are initiated by the client.
If I wanted two computers to communicate as equals, would it be right for each of them to implement a remote object? That is, the two applications would play the role of client and server.
Thank you
If you are creating desktop application, you could use sockets for communications between many computers.
A socket is one end-point of a two-way communication link between two or more programs running on the network. Socket classes are used to represent the connection between a client program and a server program. More.
Example code for server/client applications
You should know that you will need a bit of knowledge about concurrency and networking to create good communication between many computers. Creating simple server/client applications is very good way to achieve it :)
Please remember that client will always need IP:PORT address to connect to server.
I'm developing a Desktop LAN base java server and client application
where a Client must login and also to pass some data to server.
assuming i have 10 clients that inserting record simultaneously to server.
which is the best approach in this kind of situation, should I use RMI for login and record insertion? or Sockets?
if sockets please provide a key idea for me to start with.
key points to consider
-Multithreading
-able to send back data on client
If you want to connect your server via internet (and/or firewalls) it is probably a hassle to do this with plain RMI. In the past I have used Java Simon for such tasks which is very easy to implement. However if you plan to support other clients than Java clients, then you should have a look at Apache Thrift or Google Protocoll Buffers
Need to migrate from Java socket to Xmpp communication of my desktop chat application.
I got a api called SMACK for that but for that I have to use Openfire server i.e I can only code in my client. But I have my own socket server which I am using for my current chat application. Is there a way to use that server and write XMPP specific code in server?? basically reuse the socket server..?
I have written the code using SMACK which calls the server but how to make the server listen to that?
You would have to make your server understand and respond to the XMPP protocol. In other words, you would have to write an XMPP server, which makes no sense when you can simply use one of many existing ones. Using smack doesn't require you to use Openfire, but it is one of many options available.
I'm new in java, and I have a problem. I have two android phones (Client and Server). Can anybody say me how to display on Client application the Server IP address?
Kryonet is a very good Java library which provides a clean and simple API for efficient TCP and UDP client/server network communication using NIO. It works on Android as well.
It will make your network programming work a lot more easier, and you can get a better understanding of how to write client and server side code.
I would suggest that you try out your network programming skills using this library.
You need not even hard code any IP address of the server while in LAN. The clients can discover the server in just one line of code.
OK, I'm new to server-client applications, and i need some basic information, so forgive me if my question is not clear...
I want to make a chat application that would function like this:
Client A sends information to server, server sends the same information to client B, and vice versa... Think of it as of a simple chat program.
All communication is done through sockets, so i would have a server socket application, and a client socket application... I want my client application to be on my PCs and server application to be on a remote server ( it would be hosted on some free hosting websites).
My question is how do I start that server application on that remote server?
Thanks in advance!
If you are just trying to make a chat client, I don't think you would need an intermediate server. Just connect two machines using server and client sockets
SERVER:
ServerSocketChannel serverSocket;
serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind()
serverSocket.socket().accept()
CLIENT:
SocketChannel clientSocket = SocketChannel.open();
clientSocket.connect();
Of course you would have to use the bind and connect functions properly. Read up on their API's
The remote server can be started manually. (If you do not have access to remote server or if you are hosting your server on some third party infrastructure, then they might have a way to do it.)
To be able to start it remotely via some program, you again need a server on the remote machine that listens to this kind of requests.
Usually, you want an application that is running all the time at your hosting provider (like a web server or perhaps inetd) to start (or embed) your application. The details will be determined by what your hosting provider provides.
If you're using plain sockets, you should look for some remote server with SSH login. You're able to start your application on the shell then, sth like:
java -jar yourapp.jar
Free hosting websites are rather targeting customers that want to host their website. In my opinion that is not the best choice for hosting a socket application.
For developing purposes, I'd stick with the local machine for the beginning. Running/testing server/client connections on the same machine is much easier as you don't have to work on two different machines, copy code, etc.
This tutorial is relatively short, but fully covers basics of Java networking. And it is right about simple chat.