Socket server communicate with 2 clients at 1 thread? - java

A client communicate with authserver to recieve a nickname, and the auhserver send the nickname to clients and another server, in order to client communicate with the other server.
My problem is how to make the other server connect with the client and authserver without making new communication thread for each one?
public class Aatombolaserver extends Thread {
/***in order to differentiate between client socket**/
protected Socket clientSocket;
String serverHostname = new String ("127.0.0.1");
Socket authSocket = null;
/**** constructor**/
private Aatombolaserver (Socket clientSoc)
{clientSocket = clientSoc;
authSocket = clientSoc;
//store the socket of the coming client
start();//execute the run method
}
public static void main(String[] args) throws IOException
{
/**** Create a server socket and wait for a connection**/
ServerSocket serverSocket = null;
// ServerSocket serverSocket1 = null;
try {
serverSocket = new ServerSocket(9091);
// serverSocket1 = new ServerSocket(9092);
System.out.println ("Connection Socket Created");
try { System.out.println ("Waiting for a client to connect");
while (true){//wait for clients
//accept the client request and proceed with the thread creation
new Aatombolaserver(serverSocket.accept());
}
}
catch (IOException e){
System.err.println("Accept failed.");
System.exit(1);
}
} catch (IOException e) {
System.err.println("Could not listen on port: 10008.");
System.exit(1);
}//end try
/************Close Socket*******************/
finally {
try { serverSocket.close(); }
catch (IOException e){
System.err.println("Could not close port: 10008.");
System.exit(1);
}
}//end finally
}

Related

ObjectOutputStream in client causes ServerException

I have a client-server application. Right now, I'm trying to test sending messages from the client to the server and then read them from the server. I'm using ObjectInputStream and ObjectOutputStream to transfer message objects between the client and server.
However, when I try to write an object from the client, it results in a SocketException.
Server code:
while (true) {
try {
log.trace("Waiting for connection.");
Socket clientSocket = socket.accept();
log.trace("Socket connected");
/* create thread */
new Thread(new RequestRunner(clientSocket, serverID)).start();
} catch (SocketTimeoutException e) {
log.trace("Socket timed out.");
socket.close();
break;
} catch (IOException e) {
log.error("Cannot accept connection...");
break;
}
}
Server Thread:
public class RequestRunner implements Runnable {
....
public RequestRunner(Socket socket, UUID serverID) {
client = socket;
this.serverID = serverID;
}
/**
* Start the thread for the request
*/
public void run() {
log.trace("Thread started for socket");
try {
out = new ObjectOutputStream(client.getOutputStream());
in = new ObjectInputStream(client.getInputStream());
} catch (IOException e) {
log.error("Cannot intialize streams...");
return;
}
while(client.isConnected()) {
/* initialize streams */
try {
/* read message */
Object obj = in.readObject(); // does not block
MessageFrame msg = (MessageFrame) obj;
processRequest(msg);
} catch (IOException e) {
; // triggers everytime
//log.error("IO error occured while trying to get input/output stream from socket");
} catch (ClassNotFoundException e) {
log.error("Cannot read MessageFrame");
}
}
}
}
Client code:
public void init(int port) throws IOException {
log.trace("intializing to port " + port);
clientID = UUID.randomUUID();
socket = new Socket("0.0.0.0",port);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
}
public void sendEcho() throws Exception {
while(socket.isConnected()) {
try {
log.trace("Sending echo..");
msg = new EchoMessage(clientID);
curMsgID = msg.getMsgID();
out.writeObject(msg); // throws SocketException, socket closed
out.flush();
break;
} catch (SocketException e) {
log.error ("Cannot send echo.. socket closed.");
break;
} catch (IOException e) {
continue;
}
}
}
The statement out.writeObject(msg) causes a ServerSocket exception with Socket closed as the reason. And the server does not register receiving an object from in.readObject().
netstat shows the connection as established, the error occurs when I try to write the object.
What am I doing wrong ?
You should only have one InputStream and one OutputStream.
out = new ObjectOutputStream(client.getOutputStream());
in = new ObjectInputStream(client.getInputStream());
Should be:
out = client.getOutputStream();
in = client.getInputStream()
And you should change it in the client code when getting the streams from the sockets as well.

client creates new socket object everytime the server is on

i've done a socket programming with client residing on android and server residing on the desktop ....... whenever server is down as we know client lost the connection.....so it undergoes a looping until it connects to server.......
here the problem is in the below code
tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);
in the while loop in the case of lost connection.........but when the connection is on it again creates a new object........
can anyone please tell me how to solve this problem..........
In the client side
while(true){
try {
tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);
tabletout = new PrintWriter(tabletclient.getOutputStream());
in = new Scanner(tabletclient.getInputStream());
try
{
if((line = in.nextLine())!=null)
{
// my task to be done
}
}catch(Exception d){
System.out.println("Connection from server has lost.........tabletclient.isConnected()----->"+tabletclient.isConnected());
}
} catch (UnknownHostException e) { System.out.println("Entered 2.........");
} catch (IOException e) { System.out.println("Entered 3.........");e.printStackTrace();
}
}
In in the Server side
:
:
private Set <Socket> TABhs=new HashSet<Socket>();
:
:
new Thread(new TABServerThread()).start(); // runs in background
:
:
:
class ServerThread implements Runnable {
private ServerSocket server;
#Override
public void run() {
try {
server = new ServerSocket(SERVER_PORT);
System.out.println("Server Start the server at port " + SERVER_PORT
+ " and waiting for clients...");
while (true) {
Socket socket = server.accept();
System.out.println("Server Accept socket connection: "
+ socket.getLocalAddress());
new Thread(new ClientHandler(socket)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static PrintWriter out;
class ClientHandler implements Runnable {
private Socket clientSocket;
private Scanner in;
public ClientHandler(Socket clietSocket) {
this.clientSocket = clietSocket;
}
#Override
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream());
in = new Scanner(clientSocket.getInputStream());
String line;
System.out.println("ClientHandlerThread Start communication with : "+ clientSocket.getLocalAddress());
try{
while((line = in.nextLine()) != null) {
System.out.println("ClientHandlerThread Client says: " + line);
String dat[]=line.split("#");
String query="insert into table_orders (tableno,kotno, orders,status) values('"+dat[1]+"','"+dat[0]+"','"+dat[2]+"','pending')";
try {
int i= dbGetDet.insertDetails(query);
if(i>0)
{
fillTable();
filtercomboBox();
out.print("success");
out.flush();
for(Socket so:TABhs)
{
PrintWriter ot = new PrintWriter(so.getOutputStream());
ot.println("tableallocation#"+dat[1]);
ot.flush();
}
System.out.println("SENDED 'SUCCESS' TO CLIENT");
}
} catch (Exception ex) {
Logger.getLogger(MYClientclass.class.getName()).log(Level.SEVERE, null, ex);
}
// }
}
}catch(Exception r){}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the Button click of server
String stat=status_combo.getSelectedItem().toString();
String tables=tableno_combo.getSelectedItem().toString();
String kotno=kotno_combo.getSelectedItem().toString();
if(stat.equals("Processing"))
{
try {
TABhs = new CopyOnWriteArraySet(TABhs);
int soint=1;
System.out.println("TABhs Processing--------------------->"+TABhs.size());
for(Iterator <Socket> it=TABhs.iterator();it.hasNext();)
{
Socket so=it.next();
System.out.println("SEEE SOCKET Processing"+soint+"----->"+so.isClosed());
PrintWriter ot = new PrintWriter(so.getOutputStream());
ot.println("tableupdate#"+tables+"#"+kotno+"#processing");
ot.flush();
JOptionPane.showMessageDialog(rootPane, "<html><body>Table Kot Status Changed to <b>Processing</b></body></html>");
soint++;
}
System.out.println("TABhs Processing--------------------->"+TABhs.size());
}
catch (Exception ex) {
Logger.getLogger(MYClientclass.class.getName()).log(Level.SEVERE, null, ex);
}
}
NOW EACH TIME WHEN BUTTON IS CLICKED THE OUTPUT IS AS GIVEN BELOW
FISRT CLICK
SEEE SOCKET Ready 1----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 2----->false
TABhs--------------------->2
SECOND CLICK
SEEE SOCKET Ready 1----->false
SEEE SOCKET Ready 2----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 3----->false
TABhs--------------------->4
FOURTH CLICK
SEEE SOCKET Ready 1----->false
SEEE SOCKET Ready 2----->false
SEEE SOCKET Ready 3----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 4----->false
TABhs--------------------->5
I think the problem is at the client's side you read a line and then create a new connection.
I think you must keep reading the socket until it's closed or an error occurs.
For example:
while (true)
{
tabletclient = null;
int loop = 0;
// loop until a connection is established
while (tabletclient == null)
{
try
{
tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);
}
catch (Exception e)
{
e.printStackTrace();
// set the value to quit when no connection could be established
if (loop++ > 100)
return;
}
}
try
{
tabletout = new PrintWriter(tabletclient.getOutputStream());
in = new Scanner(tabletclient.getInputStream());
// read the socket until it's closed or an error occurs
try
{
while ((line = in.nextLine()) != null)
{
// my task to be done
}
}
catch (Exception d)
{
System.out.println("Connection from server has lost.........tabletclient.isConnected()----->"
+ tabletclient.isConnected());
}
tabletsocket.close();
}
catch (UnknownHostException e)
{
System.out.println("Entered 2.........");
}
catch (IOException e)
{
System.out.println("Entered 3.........");
e.printStackTrace();
}
}
Also, you must close the server side when the transfer from the server to the client is completed.

Using a ServerSocket without port forwarding?

This may be a stupid question, but here goes.
Im writing this chat program, where there is a server, and clients that can connect to it. I want to implement private messaging into the program, but I don't know how to get the clients to directly connect to eachother. For the server, I used a ServerSocket, which runs on a single port. To get that to work, I needed to forward a port to the server. Is there a way to get the clients to wait for connections, without forwarding a port to them?
Thanks
The whole point of TCP/IP is that a single client connects to a predefined port on a server. So yes, you'll also need to have a ServerSocket on the client that's going to accept the direct connection. You'll almost always run into trouble with port forwarding and the like, which is why UPnP was invented one day.
What you are trying to do is 'peer to peer' connectivity, aka P2P, which is always, by its very definition, plagued by firewalling problems. As such it's usually, especially for a chat, easier to use the central server as 'switchboard' server and relay the private messages as well.
I've written not long time ago a template for multiple client - server application, that might help you to solve your problem. The rest of your question was already answerd by #Niels, I think ;)
import java.net.*;
import java.io.*;
class ServeConnection extends Thread {
private Socket socket = null;
private BufferedReader in = null;
private PrintWriter out = null;
public ServeConnection(Socket s) throws IOException {
// init connection with client
socket = s;
try {
in = new BufferedReader(new InputStreamReader(
this.socket.getInputStream()));
out = new PrintWriter(this.socket.getOutputStream(), true);
} catch (IOException e) {
System.err.println("Couldn't get I/O.");
System.exit(1);
}
start();
}
public void run() {
System.out.println("client accepted from: " + socket.getInetAddress()
+ ":" + socket.getPort());
// get commands from client, until is he communicating or until no error
// occurs
String inputLine, outputLine;
try {
while ((inputLine = in.readLine()) != null) {
System.out.println("request: " + inputLine);
outputLine = inputLine;
out.println("I've recived "+outputLine);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("server ending");
out.close();
try {
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Server {
public static void svr_main(int port) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.err.println("Could not listen on port: " + port);
System.exit(1);
}
System.out.println("Server ready");
try {
while (true) {
Socket socket = serverSocket.accept();
try {
new ServeConnection(socket);
} catch (IOException e) {
System.err.println("IO Exception");
}
}
} finally {
serverSocket.close();
}
}
}
class Client {
static Socket echoSocket = null;
static PrintWriter out = null;
static BufferedReader in = null;
public static void cli_main(int port, String servername) throws
IOException {
try {
echoSocket = new Socket(servername, port);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + servername);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + servername);
System.exit(1);
}
System.out.println("Client ready!");
while (true) {
inputLine = (in.readLine().toString());
if (inputLine == null) {
System.out.println("Client closing!");
break;
}
// get the input and tokenize it
String[] tokens = inputLine.split(" ");
}
out.close();
in.close();
echoSocket.close();
System.out.println("Client closing");
}
}
public class MyClientServerSnippet{
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.err.println("Client: java snippet.MyClientServerSnippet<hostname> <port>");
System.err.println("Server: java snippet.MyClientServerSnippet<port>");
System.exit(1);
}
else if (args.length > 1) {
System.out.println("Starting client...\n");
Client client = new Client();
client.cli_main(3049, "127.0.0.1");
} else {
System.out.println("Starting server...\n");
Server server = new Server();
server.svr_main(3049);
}
}
}

TCP read a file and send to the client in java

Let me explain my work.
I have to send a file from server to client. In server I accepted the connection from the client and not closed the connection. Now I have to send the data as streams to the client.
Now I have selected a content and wrote in a file in method.
I have send method in which I have to send the file in inputstreams to the client. How to send?
public static void main(String args[])
{
int port=5000;
while(true)
{
try
{
ServerSocket ser=new ServerSocket(port+10);
System.out.println("CLIENT A IS CONNECTED");
ser.close();
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser1=new ServerSocket(port+20);
ser1.accept();
System.out.println("CLIENT B IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser2=new ServerSocket(port+30);
ser2.accept();
System.out.println("CLIENT C IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser3=new ServerSocket(port+40);
ser3.accept();
System.out.println("CLIENT D IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
In main method I accepted All the client request.
private void jButton1ActionPerformed1(java.awt.event.ActionEvent evt) //sendbutton
{
try
{
FileReader buf=new FileReader("e:\\input.txt");
int port= // the port of client which I selected to send the data
try
{
#SuppressWarnings("resource")
ServerSocket ser=new ServerSocket(port);
Socket soc=ser.accept();
BufferedReader toclient=new BufferedReader(buf);
DataOutputStream dos=new DataOutputStream(soc.getOutputStream());
System.out.println(dos.toString());
dos.flush();
dos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
Now my question is I already opened the ports and the connection is established in the main method. I have to send the data from server to client by selecting to which client should receive the data in sendbutton method. I am confused how to check or pass the socket object serv1 to the send method?.
below is the sample server code (please add corresponding import statements and your logic)
class SampleServer
{
private int port;
private ArrayList clientList;
public SampleServer()
{
this.port = 4444;
}
public SampleServer(int port)
{
this.port = port;
}
public void startServer()
{
ServerSocket ss = new ServerSocket(this.port);
Socket soc;
while(true)
{
soc = ss.accept();
clientList.add(soc);
}
}
public ArrayList getClientList()
{
return clientList;
}
}
Below is the sample main method (take this as a reference and create your own code)
public class Main
{
public static void main(String[] args) throws Exception
{
//Create a server
//Server has to be only one and multiple clients can connect it
SampleServer server = new SampleServer(5555);
server.startServer();
//Creating one client
Socket soc = new Socket("localhost", 5555);
//Similarly create n-number of clients and connect to the same port as that of server
//server.getClientList(); will help you get the list of the clients connected to the server
}
}

Java networking using threads, when one client leaves the other cannot communicate to the server

I'm trying to create a server in java which accepts clients. Currently, two clients can connect to the server. However, when one leaves the other client can no longer communicate with the server. I think the issue is that my clientSocket is created outside my thread, which means it must be declared as 'final' because I use an innerclass. However, if I move it into the thread it cannot create the clientSocket. Any ideas on how to fix this? Thanks in advance.
Comment: If I close the clientSocket when the client leaves it says "Broken Pipe" error because the clientSocket is final so it cannot be changed - ie the clientSocket is the same for both clients.
private BufferedWriter writer;
private LODGame game;
public Server(int port) throws Exception {
try{
// Listen on the given port.
serverSocket = new ServerSocket(port);
game = new LODGame();
}
catch(BindException e){
throw new Exception("Failed to create a server socket: "+
e.getMessage());
}
}
public Server(int port, String map) throws Exception {
try{
// Listen on the given port.
serverSocket = new ServerSocket(port);
game = new LODGame(map);
}
catch(BindException e){
throw new Exception("Failed to create a server socket: "+
e.getMessage());
}
}
public void run() throws Exception {
final ServerSocket serverSocket = getServerSocket();
while (true){
System.out.println("Listening for a client on port: "+
serverSocket.getLocalPort());
// Wait for a client to make contact.
final Socket clientSocket = serverSocket.accept();
// Contact ...
System.out.println("A client has arrived.");
Thread serverThread = new Thread(){
public void run(){
boolean quit = false;
while (!quit){
try{
// Wrap the input stream in a BufferedReader.
BufferedReader reader = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
// Wrap the output stream in a BufferedWriter.
writer = new BufferedWriter(
new OutputStreamWriter(clientSocket.getOutputStream()));
game.setWriter(writer);
game.startNewGame();
// Read lines until the client terminates.
String request = reader.readLine();
while(request != null){
// Write the length of the line as a String.
playerCommand(request);
request = reader.readLine();
}
}
catch(IOException e){
System.out.println("IOException talking to the client: "+
e.getMessage());
}
finally{
if(clientSocket != null){
System.out.println("The client has gone.");
break;
// Close the socket to the client.
//try
// {
// clientSocket.close();
// }
//catch(Exception e)
// {
// System.out.println("Error" + e.getMessage());
// System.exit(1);
// }
}
}
try
{
serverSocket.close();
}
catch(Exception e)
{
System.out.println("Error" + e.getMessage());
System.exit(1);
}
}
}
};
serverThread.start();
}
}
protected ServerSocket getServerSocket(){
return serverSocket;
}
// The socket on which the listening is done.
private final ServerSocket serverSocket;
No this has nothing to do with the clientSocket being final. Your problem is that you are closing the serverSocket in the client thread. You should be closing the clientSocket only at the end of the thread:
while (!quit) {
try {
...
} catch(IOException e){
System.out.println("IOException talking to the client: "+
e.getMessage());
} finally {
...
clientSocket.close();
}
// DON'T DO THIS: serverSocket.close();
}
The serverSocket should only be closed if accept() throws an Exception -- it should not be touched by the client thread at all.

Categories

Resources