Issue posting to multiple clients - java

Hey guys i've just recently been getting a hang of sockets but ran into an issue with posting to multiple clients. Ive made it multithreaded to handle this and am attempting to hold all client data the server needs to send to in a hashmap, however when cycling and adding to the hashmap. it seems to only ever add one person. Heres the code..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
public class HostMain extends Thread {
/**
* The set of all names of clients in the chat room. Maintained
* so that we can check that new clients are not registering name
* already in use.
*/
private static HashMap<String, ConnectedUsers> users = new HashMap<String, ConnectedUsers>();
public HostMain() throws IOException
{
}
public void run()
{
try
{
System.err.println("SERVER:The chat server is running.");
ServerSocket listener = new ServerSocket(PORT);
System.err.println("SERVER:socket created");
Constants.getInstance().getStatusLabel().setText("Server is running. Join when ready");
try {
while (true) {
System.err.println("SERVER:New handler being created");
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
catch(Exception e)
{
Constants.getInstance().getStatusLabel().setText("SERVER:A HOST IS ALREADY RUNNING ON THIS PORT!");
System.err.println("SERVER:A HOST IS ALREADY RUNNING ON THIS PORT!");
}
}
/**
* The port that the server listens on.
*/
private static final int PORT = 1337;
/**
* The appplication main method, which just listens on a port and
* spawns handler threads.
*/
/**
* A handler thread class. Handlers are spawned from the listening
* loop and are responsible for a dealing with a single client
* and broadcasting its messages.
*/
private static class Handler extends Thread {
private String name;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private ObjectInputStream oin;
private ObjectOutputStream oout;
/**
* Constructs a handler thread, squirreling away the socket.
* All the interesting work is done in the run method.
*/
public Handler(Socket socket) {
this.socket = socket;
}
/**
* Services this thread's client by repeatedly requesting a
* screen name until a unique one has been submitted, then
* acknowledges the name and registers the output stream for
* the client in a global set, then repeatedly gets inputs and
* broadcasts them.
*/
public void run() {
try {
// Create object streams for the socket.
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
oin = new ObjectInputStream(socket.getInputStream());
oout = new ObjectOutputStream(socket.getOutputStream());
//add socket to a list
users.put(name,new ConnectedUsers(name,oout));
System.err.println(users.size());
// Accept messages from this client and broadcast them.
// Ignore other clients that cannot be broadcasted to.
while (true) {
Object obj = oin.readObject();
Messages message = (Messages)obj;
if(obj.getClass().equals(Messages.class))
{
for(Map.Entry<String, ConnectedUsers> entry:users.entrySet())
{
ConnectedUsers user = entry.getValue();
user.objectWriter.writeObject(message);
}
}
}
} catch (IOException | ClassNotFoundException e) {
System.out.println(e);
} finally {
// This client is going down! Remove its name and its print
// writer from the sets, and close its socket.
if (name != null) {
users.remove(name);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}

name seems to be always null so you keep using the same key (null) which would explain why there is only one user in your map.
Also note that HashMap is not thread safe - unless you add some form of synchronization when accessing the map from your threads it could yield surprising results.
You could instead use a thread safe map, for example a ConcurrentHashMap.

Related

BufferedReader readLine() blocking until buffer is full

I'm writing a client/server pair of applications. The server runs multiple threads that collect data and adds it to a BlockingQueue. The socket code loops over the queue and sends whatever data it finds to the client. The data is a string and I append a line separator so that the client can read it using BufferedReader.readLine().
My problem is that instead of readLine() returning on each line that's available it waits until the entire buffer is full before spitting out all the complete lines in the buffer. With the default 8K buffer this means I get data via the client in 8K chunks, which is highly undesirable. I've attached MRE code that represents this. I have confirmed via logging in my actual application that the BufferedWriter is writing the data as soon as it's available from the queue, but to be honest I don't know if the delay is coming after this on the sending side, or is truly on the reading side. If you run the MRE you'll see that the data is displayed approximately 170 lines at a time by the client.
I've searched online for this phenomenon for a couple of days and the one snippet that I could find of a similar issue suggests that maybe it's something to do with the underlying InputStreamReader and/or StreamDecoder, but that is starting to get beyond my expertise. (See this link)
So my question is whether I'm implementing the BufferedReader correctly and how can I resolve the issue I'm seeing so that I get each incoming line without unnecessary delays.
package serverTest;
import java.util.concurrent.BlockingQueue;
public class ServerTest {
public static void main(String[] args) {
int port = 54321;
ServerSocketComms server = new ServerSocketComms(port);
BlockingQueue<String> queue = server.getQueue();
new Thread(server).start();
ClientSocketComms client = new ClientSocketComms("localhost", port);
new Thread(client).start();
for(int i = 0; i < 1000; i++) { // should give about 10 seconds of output
try {
queue.put("" + i + " - All work and no play makes Jack a dull boy");
// Slow things down enough to show what's happening
Thread.sleep(10);
// 48 characters should fill the 8K buffer in approximately 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package serverTest;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ServerSocketComms implements Runnable {
private final BlockingQueue<String> queue = new LinkedBlockingQueue<>();
private final int port;
public ServerSocketComms(int port) {
this.port = port;
}
#Override
public void run() {
// Open server socket and wait for connection
try {
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
// Continually loop over blocking data queue until stopped
BufferedWriter dataOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(socket.isConnected()) {
dataOut.write(queue.take());
dataOut.newLine(); // delimit strings with a line separator
}
// Loop never exits because client socket never completes because of BufferedReader issue
// so sockets never close and application never terminates
socket.close();
serverSocket.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public BlockingQueue<String> getQueue() {
// Return a reference to the sending queue to be populated by other threads
return this.queue;
}
}
package serverTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ClientSocketComms implements Runnable {
private final String server;
private final int port;
public ClientSocketComms(String server, int port) {
this.server = server;
this.port = port;
}
#Override
public void run() {
// Open socket to server and wait for incoming data
try {
Socket socket = new Socket(server, port);
BufferedReader dataIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Continually loop over incoming data until stopped
String data;
while((data = dataIn.readLine()) != null) {
// Should print out every line as it's received,
// but instead waits until buffer is full
// (outputs about 170 lines at a time)
System.out.println(data);
}
// Close socket and thread will die
// (but loop never ends because buffer doesn't get completely refilled)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Your server is using a BufferedWriter:
BufferedWriter dataOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
This one does the buffering that you do not like. It seems the default buffer size is the 8k that you are seeing although that is not documented in the API and could change. Try flushing the buffer using dataOut.flush() if at some point in time you want to ensure everything stored in the buffer so far is sent out to the client immediately. Have a look at the BufferedWriter API for details.
BTW, I have not checked whether there are any other problems in your code. But the above is definitely one.

Socket not receiving or sending message from/to server

I'm implementing a Java multi threaded server that receive messages from a client and broadcast them to the other clients, but it's not working. The server receives the client messages only when the client application closes (the client socket closes). The application is divided in two modules: client and the server. The code is a little bit long, I know that is annoying to read all, but please help me to solve this problem.
Here is the GitHub application link to facilitate the reading. Please checkout the test branch.
The server module classes:
GameServer.java
/*
* This file contains the application core server responsible to wait and accept clients connections requests.
* */
package server;
import com.sun.istack.internal.NotNull;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* #author Michael Pacheco <b>pacheco#decom.ufop.br</b>
* #version 1.0
*
* This class is responsible to receive clients connections and send messages to them.
* */
public class GameServer implements Runnable {
/**
* The port number used to start the server.
* */
private int port;
/**
* The socket used to accept clients connections.
* */
private ServerSocket serverSocket;
/**
* A {#link Logger} used to print messages for debug purpose.
* */
private final static Logger LOGGER = Logger.getLogger(GameServer.class.getName());
/**
* A hash set to store the clients sockets
* */
private HashSet<Socket> clientsSockets;
private GameServer() {
clientsSockets = new HashSet<>();
}
/**
* Instantiates a new {#link GameServer} with a given port number.
* #param port the port number used to start the server.
* */
public GameServer(int port) {
this();
this.port = port;
}
/**
* Override method from Runnable. This method is called when an attempt to close the application occur.
* */
#Override
public void run() {
Scanner s = new Scanner(System.in);
while (s.hasNext()) s.nextLine();
shutdown();
}
/**
* Start the server and listen for clients connections requests.
* */
public void start () {
try {
LOGGER.log(Level.INFO, "Trying to start the server...\n");
serverSocket = new ServerSocket(this.port);
final String ip = InetAddress.getLocalHost().getHostAddress();
LOGGER.log(Level.INFO, "Server started!\n\tPort: {0}\n\t IP: {1}\n", new Object[] {port, ip});
LOGGER.log(Level.INFO, "Press Ctrl-D to shutdown the server!\n");
waitForConnections();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to initialize the server! {0}\n", e.getMessage());
e.printStackTrace();
}
}
/**
* Wait for clients connections requests
* */
private void waitForConnections() {
new Thread(this).start();
try {
//noinspection InfiniteLoopStatement
while (true) {
Socket clientSocket = serverSocket.accept();
LOGGER.log(Level.INFO, "New client connected! {0}\n", clientSocket);
clientSocket.getOutputStream().write("You're now connected to the server\n".getBytes());
clientSocket.getOutputStream().flush();
allocateClient(clientSocket);
}
} catch (IOException e) {
// No need for printing stacktrace if the serverSocket was closed by the shutdown method
if (!serverSocket.isClosed())
e.printStackTrace();
}
}
/**
* This method is responsible to delegate the communication with the client to the {#link ClientListener}.
* #param clientSocket the client socket to delegate.
* */
private void allocateClient(#NotNull Socket clientSocket) {
clientsSockets.add(clientSocket);
new Thread(new ClientListener(clientSocket, this)).start();
}
/**
* Shutdown the server
* */
private void shutdown () {
try {
LOGGER.log(Level.INFO, "Trying to shutdown the server...\n");
// TODO Clear resources
for (Socket soc : clientsSockets) removeClient(soc);
serverSocket.close();
LOGGER.log(Level.INFO, "Server successfully shut down!\n");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to shutdown the server! {0}\n", e.getMessage());
e.printStackTrace();
}
}
/**
* Send a message to a single client.
* #param message the message to be sent.
* #param clientSocket the socket of the client that will receive the message
* */
private void sendMessage (#NotNull Object message, #NotNull Socket clientSocket) {
try (PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true)) {
writer.println(message);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Send a message to all clients except the given one.
* #param message the message to be sent.
* #param excludedClient the client that won't receive the message.
* */
void broadcast (#NotNull Object message, #NotNull Socket excludedClient) {
for (Socket client : clientsSockets) {
if (excludedClient == client)
continue;
sendMessage(message, client);
}
}
/**
* Remove the given client from server.
* #param clientSocket the client to be removed.
* */
void removeClient (#NotNull Socket clientSocket) {
try {
clientSocket.close();
clientsSockets.remove(clientSocket);
LOGGER.log(Level.INFO, "Client removed! {0}\n", clientSocket);
// TODO broadcast the client disconnection
} catch (IOException e) {
e.printStackTrace();
}
}
}
ClientListener.java
package server;
import com.sun.istack.internal.NotNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* #author Michael Pacheco <b>pacheco#decom.ufop.br</b>
* #version 1.0
*
* This class is responsible to listen messages of a single client and send them to the server and then to the other clients.
* */
public class ClientListener implements Runnable {
/**
* The socket used to communicate with the delegated client.
* */
private Socket clientSocket;
/**
* A reference to the {#link GameServer} used to call the {#link GameServer} broadcast method.
* #see GameServer
* */
private GameServer server;
/**
* A {#link Logger} used to print messages for debug purpose.
* */
private final static Logger LOGGER = Logger.getLogger(ClientListener.class.getName());
/**
* Instantiate a new {#link ClientListener} with a given client socket.
*
* #param clientSocket the socket of the delegated client.
* #param server the server reference used to call the broadcast method.
* */
ClientListener(#NotNull Socket clientSocket, #NotNull GameServer server) {
this.clientSocket = clientSocket;
this.server = server;
}
/**
* Listen for client messages and send it to the server.
* */
#Override
public void run() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) {
String message;
while ((message = reader.readLine()) != null) {
// send received message to the server
LOGGER.log(Level.INFO, "Message received!\n\t From: {0}\n\tMessage: {1}\n",
new Object[]{clientSocket, message});
server.broadcast(message, clientSocket);
}
} catch (IOException e) {
if (!clientSocket.isClosed())
e.printStackTrace();
} finally {
// send the client to server to be disconnected
server.removeClient(clientSocket);
}
}
}
The client module classes:
GameClient.java
package client;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GameClient {
public static void main(String[] args) {
final String serverAddress = args.length == 2 ? args[0] : "localhost";
final int port = args.length == 2 ? Integer.parseInt(args[1]) : 5000;
final Logger LOGGER = Logger.getLogger(GameClient.class.getName());
try {
Socket serverSocket = new Socket(serverAddress, port);
LOGGER.log(Level.INFO, "Connection successful! {0}\n", serverSocket);
new Thread(new ClientWriterThread(serverSocket)).start();
new Thread(new ClientReaderThread(serverSocket)).start();
} catch (IOException e) {
LOGGER.log(Level.SEVERE,"Failed to connect with the server\n", e);
e.printStackTrace();
}
}
}
ClientWriterThread.java
package client;
import com.sun.istack.internal.NotNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* #author Michael Pacheco
* #version 1.0
* This class is responsible to read client messages and send it to the server.
* */
public class ClientWriterThread implements Runnable {
/**
* The socket used to send messages to the server.
* */
private Socket serverSocket;
/**
* Instantiate a new {#link ClientReaderThread} with a given server socket.
* #param serverSocket the socket used to send messages to the server.
* */
ClientWriterThread(#NotNull Socket serverSocket) {
this.serverSocket = serverSocket;
}
/**
* Read messages typed by the client and send it to the server.
* */
#Override
public void run() {
try {Thread.sleep(1000);}
catch (InterruptedException e) {e.printStackTrace();}
BufferedReader keyboardReader = null;
try (PrintWriter socketWriter = new PrintWriter(serverSocket.getOutputStream(), true)) {
keyboardReader = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = keyboardReader.readLine()) != null) socketWriter.write(line);
} catch (IOException e) {
if (!serverSocket.isClosed())
e.printStackTrace();
} finally {
try {
if (keyboardReader != null) keyboardReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ClientReaderThread.java
package client;
import com.sun.istack.internal.NotNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* #author Michael Pacheco
* #version 1.0
* This class is responsible to read messages sent by the server and show them to the client.
*/
public class ClientReaderThread implements Runnable {
/**
* The socket used to read messages sent by the server.
*/
private Socket serverSocket;
ClientReaderThread(#NotNull Socket serverSocket) {
this.serverSocket = serverSocket;
}
/**
* Read messages sent by the server.
* */
#Override
public void run() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()))) {
String message;
while ((message = reader.readLine()) != null) System.out.println(message);
} catch (IOException e) {
if (!serverSocket.isClosed())
e.printStackTrace();
}
}
}
Be sure to PrintWriter.flush() the messages you send after each write() in ClientWriterThread.java.
As a side note, in order to make the code clearer change your serverSocket variable name to clientSocket in the appropriate classes (GameClient.java and ClientWriterThread.java).
You use PrintWriter with autoflush, but you don't use println(...) or format(...). The .write() is not subject to the autoflush.
FYI, clientsSockets is not thread safe, you add/remove on distinct threads. Not necessarily the bug, but careful. Also, spinning non-daemon threads without keeping a reference is risky; always keep threads under control and don't count too much on interrupt()/InterruptedException/InterruptedIOException...

Java: socket not getting input

So, I'm making a tic tac toe project and I'm having a problem with the network part, it's all finished, only missing the part that connects players with each other, this is the class with the problem:
public class Enemy implements Runnable{
private static Socket enemy;
public Enemy(Socket sock){
enemy = sock;
}
public static void passaJogada(int xPos, int yPos){
try {
PrintWriter saida = new PrintWriter(enemy.getOutputStream());
String x = "" + xPos;
saida.println(x);
String y = "" + yPos;
System.out.print(x + y);
saida.println(y);
} catch (IOException e) {
System.out.println("Ocorreu um erro!");
}
}
public void run() {
try {
BufferedReader entrada = new BufferedReader(new InputStreamReader(enemy.getInputStream()));
while(!EndGameWindow.getEnd()) {
int x = Integer.parseInt(entrada.readLine());
int y = Integer.parseInt(entrada.readLine());
GameWindow.gameButton[x][y].fazerJogada(x,y);
}
entrada.close();
} catch (IOException e) {
System.out.println("Um errro ocorreu!");
}
}
}
and I don't have a clue of what is going on, all I know is that the PrintWriter is writing but the BufferedReader is not reading.
Just ignore the portuguese name of the variables and methods.
See the API for PrintWriter, in particular the single parameter OutputStream constructor you are using:
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
In other words, the PrintWriter is buffered and needs to be flushed. To enable automatic line flushing, use the appropriate constructor
PrintWriter saida = new PrintWriter(enemy.getOutputStream(), true);
...or explicitly flush the PrintWriter:
....
saida.println(y);
saida.flush();
I am assuming that you want players to play with each other even if none of them are sitting on the computer where the server "game" is. In that case what you should do is to separate the game from the networking. I am going to also assume that you have all the game part working like it should. So here is what you can do.
1) First make a class where where only the connecting to the server part is taking place , you can name it server(look below in example code).
2) Make another class to make object of these clients (in your case players).(again look at the code below)
3) Then you can have them interact with the game.
If you are having trouble fixing it, let me know maybe i can help you with more detail.
You simple server part .
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* Created by baljit on 03.05.2015.
* #author Baljit Sarai
*/
public class Server {
ServerSocket serverSocket;
public static List<ClientConnection> clientConnectionList = new ArrayList<ClientConnection>();
public Server() {
try {
serverSocket = new ServerSocket(12345);
System.out.println("Server started");
} catch (IOException ioe) {
System.out.println("Something went wrong");
}
}
public void serve(){
Socket clientSocket;
try {
while((clientSocket = serverSocket.accept()) != null){
System.out.println("got client from"+clientSocket.getInetAddress());
ClientConnection clientConnection = new ClientConnection(clientSocket);
clientConnectionList.add(clientConnection);
ClientHandler clientHandler = new ClientHandler(clientConnection,serverSocket);
clientHandler.start();
}
}catch (IOException ioe){
System.out.println("Something went wrong");
}
}
}
The clientConnection in this case can be the pllayer object you should make just to make things easier for your self.
The clientHandler is the class that can handle each player at the same time.
The "clientConnectionList" here is just to store the connections so you know where to find them.
So here is the ClientConnection class.
import java.io.*;
import java.net.Socket;
/**
* Created by baljit on 03.05.2015.
* #author Baljit Sarai
*/
public class ClientConnection {
private Socket connection;
private DataInputStream streamIn;
private DataOutputStream streamOut;
private BufferedInputStream bufferedInputStream;
public ClientConnection(Socket socket){
connection = socket;
try{
bufferedInputStream = new BufferedInputStream(connection.getInputStream());
streamIn = new DataInputStream(bufferedInputStream);
streamOut = new DataOutputStream(connection.getOutputStream());
}catch (IOException ioe){
System.out.println("Something went wrong when trying create the Connection Object");
}
}
public boolean isBound(){
return connection.isBound();
}
public DataInputStream getStreamIn() {
return streamIn;
}
public DataOutputStream getStreamOut() {
return streamOut;
}
public void close(){
try{
connection.close();
}catch (IOException ioe){
System.out.print("Something went wrong trying to close the connection");
}
}
public String getAddress(){
return connection.getInetAddress().toString();
}
}
And here is the ClientHandler Class
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by baljit on 03.05.2015.
* #author Baljit Sarai
*/
public class ClientHandler extends Thread{
private ServerSocket serverSocket;
String data;
ClientConnection connection;
public ClientHandler(ClientConnection clientConnection, ServerSocket serverSocket) {
connection = clientConnection;
}
public void makeMove(String data){
//This is where you put the logic for how the server can
//interpetate the readed data to a game move
}
#Override
public void run(){
System.out.println("ClientHandler running");
try{
while(connection.isBound()){
data= connection.getStreamIn().readUTF();
this.makeMove(data);
}
connection.close();
}catch (IOException ioe){
System.out.println("Connection dead "+connection.getAddress());
Server.clientConnectionList.remove(connection);
}
}
}
Please let me know how it is working out for you. Maybe i can help you better :) good luck :)

Java Sockets - send data from server to client

I have a technogical question regarding Java Sockets.
For example lets assume I have one Java Sockets Server and n multiple clients.
Is it possible to send data from the Server to any or all Clients in nearly realtime?
More precisely:
Is there kind of Listener which can be implemented in a Sockets Client?
or do I need a loop in the client code where I ask every x milliseconds if there is a task for me?
Can somebody tell me what approach would be the best one?
And in addition if somebody has a code example I would be happy too.
Thanks!
You can establish a Thread in your client which listens to the socket and waits. It will continue when it gets data from the server.
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
public class ResponseThread extends Thread {
private final Socket socket;
/**
* Client is a selfmade interface which has no direct connection to socket communication.
* i build it to provide start(), stop() and isRunning() methods.
*/
private final Client client;
/**
* #param client encapsulated methods to check and manage the client status itself.
* #param socket the socket which is connected to the server.
*/
public ResponseThread(final Client client, final Socket socket) {
this.client = client;
this.socket = socket;
}
#Override
public void run() {
ObjectInputStream reader = null;
try(ObjectInputStream reader = new ObjectInputStream(socket.getInputStream())) {
while (client.isRunning()) {
try {
// The thread will wait here until the server sends data.
final String line = (String) reader.readObject();
if (null == line || line.isEmpty()) {
client.stop();
} else {
System.out.println(line);
}
} catch (IOException | ClassNotFoundException e) {
client.stop();
}
}
} catch (IOException ex) {
System.out.println("ERROR Abort reading. Could not establish InputStream from Socket.");
} finally {
try {
reader.close();
} catch (IOException ex) {
System.out.println("FATAL Could not close Socket.InputStream.");
}
}
}
public Socket getSocket() {
return socket;
}
}

proxy server in java

i have the follwoing code of proxy server. IS if the right approach? Will this be able to handel load/trafffic if deployed comerially??
package proxyserver;
import com.sun.corba.se.spi.activation.Server;
import java.net.* ;
import java.io.* ;
import java.lang.* ;
import java.util.* ;
/**
*
* #author user
*/
public class Main {
/**
* #param args the command line arguments
*/
// Variable to track if an error occurred
boolean errorOccurred = false;
//Variables for the host and port parameters
public static void main(String[] args) {
// TODO code application logic here
int localPort = -1;
int remotePort = -1;
String remoteHost = "www.youtube.com";
System.out.print("dwdsw");
Integer parseLocalPort = new Integer(555);
Integer parseRemotePort = new Integer(80);
localPort =80 ;
remotePort = 80;
//Create a listening socket at proxy
ServerSocket server = null;
try
{
server = new ServerSocket(localPort);
}
catch(IOException e)
{
System.err.println("Error: " + e.getMessage());
System.exit(-1);
}
//Loop to listen for incoming connection,
//and accept if there is one
Socket incoming = null;
Socket outgoing = null;
while(true)
{
try
{
// Create the 2 sockets to transmit incoming
// and outgoing traffic of proxy server
incoming = server.accept();
outgoing = new Socket(remoteHost, remotePort);
// Create the 2 threads for the incoming
// and outgoing traffic of proxy server
ProxyThread thread1 = new ProxyThread(incoming, outgoing);
thread1.start();
ProxyThread thread2 = new ProxyThread(outgoing, incoming);
thread2.start();
}
catch (UnknownHostException e)
{
System.err.println("Error: Unknown Host " + remoteHost);
System.exit(-1);
}
catch(IOException e)
{
//continue
System.exit(-2);;
}
}
}
}
now proxy classs
package proxyserver;
/**
*
* #author user
*/
import java.net.* ;
import java.io.* ;
import java.lang.* ;
import java.util.* ;
class ProxyThread extends Thread
{
Socket incoming, outgoing;
ProxyThread(Socket in, Socket out)
{
incoming = in;
outgoing = out;
}
// Overwritten run() method of thread,
// does the data transfers
public void run()
{
byte[] buffer = new byte[5000];
int numberRead = 0;
OutputStream toClient;
InputStream fromClient;
try{
toClient = outgoing.getOutputStream();
fromClient = incoming.getInputStream();
while(true)
{
numberRead = fromClient.read(buffer, 0, 50);
if(numberRead == -1)
{
incoming.close();
outgoing.close();
}
String st = new String(buffer,"US-ASCII");
System.out.println("\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\nXXXXXXXXXXXXXXXX\n\n" + st);
toClient.write(buffer, 0, numberRead);
}
}
catch(IOException e)
{
}
catch(ArrayIndexOutOfBoundsException e)
{
}
}
}
[ OK ... enough teasing :-) ]
It looks like it should work, though:
The stuff that you print to System.err in the Proxy class may be mangled. (As I said before, you cannot just assume that every web page is encoded in ASCII!!)
You should probably be reading much more than 50 bytes at a time .... especially if you want high throughput.
Your main class probably should be using a thread pool rather than creating and throwing away threads. And you probably should put an upper bound on the number of threads you want to allow at any given time.
You probably need to do something about servers that take a long time to deliver their responses, etcetera.
Finally, in response to this:
Will this be able to handle the
load/traffic if it is deployed commercially??
It is impossible to say how much load you could pump through this program. For a start, it will depend on your processor and network interface hardware.
It looks about right in principle but you should take a look at an open source version like TCP Proxy for pointers on maximizing throughput, increasing resilience, etc.

Categories