Hi i am trying to get some data about a socket client connecting to a mutltythreaded server process in the same machine. The server thread is triggered correctly and the client ip is retreived ok, but i cant seem to be able to send a string through the connection.
THE CLIENT
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package solverapplet;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
/**
*
* #author me
*/
public class Solverapplet {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Socket s;
try {
s = new Socket("IP", 4445);
System.out.println(s.getPort());
//DataInputStream in = new DataInputStream (s.getInputStream());
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
out.write("gamma");
out.newLine();
out.flush();
} catch (UnknownHostException ex) {
Logger.getLogger(Solverapplet.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Solverapplet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
THE SERVER THREAD
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serversideserver;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.List;
import java.sql.*;
import com.google.gson.Gson;
class doComms implements Runnable {
private Socket server;
private String line,input,ip;
doComms(Socket server, String ip) {
this.server=server;
this.ip=ip;
}
public void run () {
input="";
try {
// Get input from the client
BufferedReader in = new BufferedReader(
new InputStreamReader(server.getInputStream()));
PrintStream out = new PrintStream(server.getOutputStream());
Connection conn = null;
try
{
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
// create the java statement
Statement st = conn.createStatement();
// ResultSet rs;
while((line = in.readLine()) != null && !line.equals(".")) {
// Now do the magic.
//Data data = new Gson().fromJson(line, Data.class);
System.out.println("LINE: " + line);
input=line;
st.executeUpdate("UPDATE `solvers` SET `online`='1',`ip`='"+ server.getInetAddress().toString().substring(1) +"' WHERE `user`='"+ line +"'");
// input= data.getcmd();
out.println("{\"captcha\":1,\"text\":\"abc\",\"is_correct\":\"true\"}");
}
}
catch (Exception e)
{
System.out.println (e.toString());
}
// Now write to the client
System.out.println("UPDATE `solvers` SET `online`='1',`ip`='"+ server.getInetAddress() +"' WHERE `user`='"+ input +"'");
//out.println("Overall message is:" + input);
server.close();
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
The line sent through is empty.
CONNECTION ESTABLISHED
package serversideserver;
import java.io.*;
import java.net.*;
import java.security.*;
import java.sql.*;
/**
* Title: Sample Server
* Description: This utility will accept input from a socket, posting back to the socket before closing the link.
* It is intended as a template for coders to base servers on. Please report bugs to brad at kieser.net
* Copyright: Copyright (c) 2002
* Company: Kieser.net
* #author B. Kieser
* #version 1.0
*/
public class Serversideserver {
private static int port=4445,portsolver=4445, maxConnections=0;
// Listen for incoming connections and handle them
public static void main(String[] args) {
int i=0;
try{
ServerSocket listener = new ServerSocket(port);
Socket server;
long counter=0;
int counter1=0;
int id=0;
String ip="uninit";
while((i++ < maxConnections) || (maxConnections == 0)){
server = listener.accept();
counter++;
doComms conn_c= new doComms(server,ip);
Thread t = new Thread(conn_c);
t.start();
//System.out.println("counter "+ (counter % id) );
}
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
I looks like you've got a bit of a timing issue. The following is your code with correct timing. Note that I removed code unnecessary to the issue at hand.
Client: It looks like in the client, you were writing to the socket and immediately terminating the application (causing the connection to close). The doComms class was writing back to the client so I've added code to read the response. If, however, you were not expecting a response, you would still want to read in a byte. This will allow you to make sure you got the EOF rather than some data, and it blocks the current thread and keeps the connection alive.
package solverapplet;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
/**
*
* #author you
*/
public class Solverapplet {
/**
* #param args the command line arguments
* #throws IOException
*/
public static void main(String[] args) throws IOException {
Socket s = null;
try {
// make connection
s = new Socket("localhost", 4445);
// define streams
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
// write data
out.write("gamma");
out.newLine();
out.flush();
// read response
String returnData = in.readLine();
System.out.println(returnData);
} catch (UnknownHostException ex) {
Logger.getLogger(Solverapplet.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Solverapplet.class.getName()).log(Level.SEVERE, null, ex);
} finally {
// close connection
s.close();
}
}
}
Server: I've changed the server to allow a maximum number of connections at any given time rather than shutting down after the maximum number of connections has been established. Also, note that I the threads are not daemons. If you want to serve X number clients then shutdown, you need a mechanism to allow the threads to continue executing before shutting down ServerSocket
package serversideserver;
import java.io.*;
import java.net.*;
/**
* Title: Sample Server
* Description: This utility will accept input from a socket, posting back to the socket before closing the link.
* It is intended as a template for coders to base servers on. Please report bugs to brad at kieser.net
* Copyright: Copyright (c) 2002
* Company: Kieser.net
* #author B. Kieser
* #version 1.0
*/
public class Serversideserver {
private static int port =4445;
private static int maxConnections =10;
private static int connections = 0;
synchronized static void connectionClosed() {
connections--;
Serversideserver.class.notify();
}
/**
* The blocking mechanism to only allow <code>maxConnections<code>
* #throws InterruptedException
* thrown if blocking thread is interupted
*/
private synchronized static void nextConnection() throws InterruptedException {
while(connections>=maxConnections) {
Serversideserver.class.wait();
}
}
public static void main(String[] args) throws InterruptedException {
try{
// server socket
ServerSocket listener = new ServerSocket(port);
// socket
Socket socket;
// Keep the server alive
while(true){
// Blocks if we have reached the max
nextConnection();
// Accept connection to client
socket = listener.accept();
// define request service
doComms conn_c= new doComms(socket,socket.getInetAddress().getCanonicalHostName());
Thread t = new Thread(conn_c);
t.setDaemon(false);
// run request service
t.start();
}
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
doComms: Not much has changed with this class... I just cleaned it up a bit and removed unnecessary lines of code.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serversideserver;
import java.io.*;
import java.net.*;
class doComms implements Runnable {
private Socket socket;
private String ip;
doComms(Socket socket, String ip) {
this.socket = socket;
this.ip = ip;
}
public void run () {
try {
// Define input/output
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream out = new PrintStream(socket.getOutputStream());
// Process requests until the EOF is found
String line;
while((line = in.readLine()) != null && !line.equals(".")) {
// print input
System.out.println("LINE: " + line);
// print process line
System.out.println("UPDATE `solvers` SET `online`='1',`ip`='"+ ip +"' WHERE `user`='"+ line +"'");
// write response
out.println("{\"captcha\":1,\"text\":\"abc\",\"is_correct\":\"true\"}");
}
socket.close();
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
} finally {
Serversideserver.connectionClosed();
}
}
}
Hope this helps :)
Your question is seriously vague, but if you're wondering why the server can't reply to your client, it's because the client never reads from the socket.
Related
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...
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 :)
When I close client it gives me error on server side
Like : java.net.SocketException: Connection reset at
java.net.SocketInputStream.read(SocketInputStream.java:196) at
java.net.SocketInputStream.read(SocketInputStream.java:122) at
java.net.SocketInputStream.read(SocketInputStream.java:210) at
java.io.DataInputStream.readLine(DataInputStream.java:513) at
HServer.run(HServer.java:28)
Checkout the code and please tell me how can I send one message of client to all other clients using threads please help me guys :)
ClientDemo.java
import java.io.*;
import java.net.Socket;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author zain-mughal
*/
public class ClientDemo extends javax.swing.JFrame {
DataInputStream input;
PrintWriter output;
public void Connect(){
try {
Socket s = new Socket("127.0.0.1", 2111);
input=new DataInputStream(s.getInputStream());
output=new PrintWriter(s.getOutputStream());
boolean more_data=true;
while(more_data){
String in=input.readLine();
if(in==null){
more_data=false;
}
else{
System.out.println(in);
}
}
s.close();
input.close();
output.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String args[]){
ClientDemo obj=new ClientDemo();
obj.Connect();
}
}
HServer.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author zain-mughal
*/
public class HServer extends Thread {
Socket AcceptReQ;
public HServer(Socket reqAccept) {
this.AcceptReQ=reqAccept;
}
#Override
public void run(){
try {
DataInputStream input = new DataInputStream(AcceptReQ.getInputStream());
PrintWriter output = new PrintWriter(AcceptReQ.getOutputStream());
String in=null;
while((in=input.readLine())!=null){
System.out.println(in);
}
AcceptReQ.close();
input.close();
output.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String args[]){
try {
ServerSocket req = new ServerSocket(2111);
while(true){
Socket reqAccept=req.accept();
Thread t=new HServer(reqAccept);
t.start();
}
} catch (Exception ex) {
Logger.getLogger(HServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Try this:
Build a robust Java server
Building a basic Echo Server and A ChatServer
I used this as base for my own server.
It's a little bit old and many Java features are not used but it works great.
Take some time to comprehend it.
So I don't know who to go about creating a multithreaded server. I have client and server working fine together but can't introduce multiple clients properly. Here is my Server code:
package dod;
import java.net.*;
import java.io.*;
import dod.game.GameLogic;
public class Server{
Server(GameLogic game, int port) throws IOException{
ServerSocket ss = null;
Socket sock = null;
try{
ss = new ServerSocket(4444);//port no.
while(true){
try{
sock = ss.accept();
ClientThread thread = new ClientThread(game, sock);
System.out.println("Adding new player...");
thread.run();
}catch(final Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}catch(Exception d){
System.out.println(d);
}finally{
if(ss!=null){
ss.close();
}
}
}
}
Here is my thread class:
package dod;
import java.io.*;
import java.net.Socket;
import dod.game.GameLogic;
import dod.game.PlayerListener;
public class ClientThread extends CommandLineUser implements PlayerListener, Runnable{
DataInputStream in;
PrintStream out;
// The game which the command line user will operate on.
// This is private to enforce the use of "processCommand".
ClientThread(GameLogic game, Socket sock) {
super(game);
try{
in = new DataInputStream(sock.getInputStream());
out = new PrintStream(sock.getOutputStream());
}catch(IOException ioe){
System.out.println(ioe);
}
game.addPlayer(this);
}
/**
* Constantly asks the user for new commands
*/
public void run() {
System.out.println("Added new human player.");
// Keep listening forever
while(true){
try{
// Try to grab a command from the command line
final String command = in.readLine();;
// Test for EOF (ctrl-D)
if(command == null){
System.exit(0);
}
processCommand(command);
}catch(final RuntimeException e){
System.err.println(e.toString());
System.exit(1);
} catch (final IOException e) {
System.err.println(e.toString());
System.exit(1);
}
}
}
/**
* Outputs a message to the player
*
* #param message
* the message to send to the player.
*/
public void outputMessage(String message) {
out.print(message);
}
}
Not asking for new code as such, just need pointers as to what I need to do have multiple client connection at the same time! Thanks to anyone who helps out!
To start, add new Thread(clientThread) in the server and call start() on it - as is everything's happening on the same thread.
public class Server{
Server(GameLogic game, int port) throws IOException{
ServerSocket ss = null;
Socket sock = null;
try{
ss = new ServerSocket(4444);//port no.
while(true){
try{
sock = ss.accept();
ClientThread thread = new ClientThread(game, sock);
System.out.println("Adding new player...");
thread.start(); //you have to use start instead of run method to create multi thread application.
}catch(final Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}catch(Exception d){
System.out.println(d);
}finally{
if(ss!=null){
ss.close();
}
}
}
}
You have to use start instead of run method to create multi thread application.
If you want to send messages about new connections you have to hold sock in a list and when a new connection accepted send message to all socket object in list. (server broadcasts to all connected sockets)
I hope it helps.
I'm trying to handle multiple connections on the same port of my server. I'm doing this by instantiating an Object and passing it into the constructor for another class, which implements Runnable. Then I set up a socket in the Runnable class and call notify() on the passed Object after a Client connects on the port. This should then allow the server to restart its loop, creating another instance of the Runnable class after being notified. However, currently the wait() isnt being reached until after the client is closed. Here are the 3 relevant classes I have:
Server class:
package server;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.HashMap;
public class Server {
public static void main(String[] args){
HashMap<String, PortDummy> portDummies = new HashMap<String, PortDummy>();
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8000);
} catch (IOException e1) {
e1.printStackTrace();
}
for(;;){
Object block = new Object();
PortDummy dummy = new PortDummy(serverSocket, block, portDummies);
System.out.println("Running dummy port...");
dummy.start();
try {
synchronized(block){
System.out.println("Waiting...");
block.wait();
System.out.println("Block notified.");
}
} catch (InterruptedException e) {
System.out.println("Can't be interrupted!");
e.printStackTrace();
}
}
}
}
PortDummy (Runnable) class:
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
public class PortDummy extends Thread {
private Object block;
private HashMap<String, PortDummy> portDummies;
private String clientName = null;
ServerSocket serverSocket;
BufferedReader socketIn;
PrintWriter socketOut;
public PortDummy(ServerSocket serverSocket, Object block, HashMap<String, PortDummy> portDummies){
this.block = block;
this.portDummies = portDummies;
this.serverSocket = serverSocket;
}
#Override
public void run() {
try {
System.out.println("Starting dummy port...");
Socket clientSocket = serverSocket.accept();
System.out.println("Connection made.");
synchronized(block){
System.out.print("Notifying...");
block.notify();
System.out.println("...done.");
}
socketIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
socketOut = new PrintWriter(clientSocket.getOutputStream(), true);
String inContent;
boolean loggedIn = false;
while((inContent = socketIn.readLine()) != null){
socketOut.println("Server Echo: " + inContent);
if(inContent.startsWith("/exit")){
if(loggedIn){
portDummies.remove(clientName);
System.out.println(clientName + " signed out. Removed from portDummies.");
}
else{
System.out.println("Closing...");
}
}
else if(inContent.startsWith("/register")){
System.out.println("/register accepted");
if(!loggedIn){
if(registerUser(inContent)){
System.out.println("Successfully registered.");
socketOut.println(clientName + " successfully registered.");
loggedIn = true;
}else{
socketOut.print("That user already exists.");
}
}
else{
socketOut.print("Already logged in.");
}
}
else if(inContent.startsWith("/tell")){
if(!loggedIn){
socketOut.println("You need to log in.");
}
else{
String[] parts = inContent.split("\\w");
String[] withoutCommand = new String[parts.length-1];
for(int i = 1; i<parts.length-1; i++){
withoutCommand[i] = parts[i];
}
String[] messageParts = new String[withoutCommand.length-1];
String message = "";
for(int j = 1; j<withoutCommand.length-1; j++){
message += withoutCommand[j] + " ";
}
String recipient = withoutCommand[0];
sendMessage(recipient, message);
}
}
else if(inContent.startsWith("/help")){
socketOut.print("/help ~~~~~~~ List all commands. \n " +
"/register <username> ~~~~~~~ Register a new user with 'username'. \n " +
"/tell <username> <message> ~~~~~~~ Send 'username' text 'message'. \n " +
"/exit ~~~~~~~ Log out.");
}
}
System.out.println("Shutting down client connections...");
socketOut.close();
socketIn.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
System.out.println("IOException!");
e.printStackTrace();
}
}
private boolean registerUser(String text){
System.out.println("Registering user...");
String user = text.substring(10);
if((user != null) && !(portDummies.containsKey(user))){
portDummies.put(user, this);
clientName = user;
System.out.println(user + " registered.");
return true;
}
return false;
}
private void sendMessage(String username, String message){
if(portDummies.containsKey(username)){
PortDummy recip = portDummies.get(username);
recip.getSocketOutput().println(clientName + ": " + message);
}
else{
socketOut.write("User " + username + " doesn't exist.");
}
}
public PrintWriter getSocketOutput(){
return socketOut;
}
}
Client class:
package client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.io.IOException;
public class Client {
protected String username;
public static void main(String[] args){
try{
Socket serverSocket = new Socket("localhost", 8000);
BufferedReader socketIn = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
PrintWriter socketOut = new PrintWriter(serverSocket.getOutputStream(), true);
Scanner keyboardInputScanner = new Scanner(System.in);
String keyboardInput, serverInput;
System.out.println("Welcome to Chris Volkernick's Server IM Client! \n" +
"Type '/register <username>' to register, '/list' to list connected users," +
"\n or '/tell <username> <message>' to send a user a message. '/help' lists these commands. (Type '/exit' to sign out.)");
while((keyboardInput = keyboardInputScanner.nextLine()) != null){
System.out.println("Input '" + keyboardInput + "' read on client side.");
if(keyboardInput.equals("/exit")){
socketOut.println("/exit");
socketOut.close();
socketIn.close();
serverSocket.close();
}else{
socketOut.println(keyboardInput);
while((serverInput = socketIn.readLine()) != null){
System.out.println(serverInput);
}
}
}
keyboardInputScanner.close();
}catch(IOException e){
System.out.println("IOException!");
e.printStackTrace();
}
}
}
Am I doing something wrong with the wait() and/or notify()?
EDIT: I also tried changing the implements Runnable to extends Thread then changing the .run() in the server to .start(), but that gives me this error:
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
at server.PortDummy.run(PortDummy.java:28)
EDIT 2: It seems to be working the way it should now in terms of starting new threads. However, I'm now running into another problem: After I enter a command on the client side of any given client, I can't enter additional commands. The first command will work fine (minus /exit; haven't quite figured out how that should work yet), just can't do anything after that. For example, I can register (sign in) but after that nothing else. I can go into another instance of Client and list all the current users (works), but again, after that I cannot enter additional commands. Any idea what may be happening to cause this?
The problem is that your child threads are trying to listen on port 8000, but the parent thread is already doing that. What you need to do is pass accept a connection from the original socket and then give it to the child thread. I'm not exactly sure how to do this in Java, but I suspect it's just..
Put this in your main thread:
ServerSocket serverSocket = new ServerSocket(8000);
Socket clientSocket = serverSocket.accept();
And then once you get that, pass clientSocket to your Thread.
That way there's only one socket listening on port 8000, but you can make child threads handle each connection.
When using wait and notify, realize that notifies aren't queued, so if the notify happens before the wait occurs, you will never exit the wait. Therefore you should never perform naked waits, that is there should always be some condition you test to see if you should wait.
sychronized(block) {
while (!available) {
block.wait();
}
}
and
synchronized(block) {
available = true;
block.notifyAll();
}
etc
package so_7775790;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;
/**
* barebones server -- ctl-C to kill it ;)
*/
public class Server implements Runnable {
final static Logger log = Logger.getLogger(Server.class.getSimpleName());
public static void main(String[] args) {
final int port = 8000;
final String tname = "my-server-thread";
final Server server = new Server(port);
try {
Thread tserver = new Thread(server, tname);
tserver.start();
tserver.join();
} catch (Exception e) {
log.severe(e.getMessage());
}
}
// -------------------------------------------------
// server
// -------------------------------------------------
final int port;
public Server(int port) {
this.port = port;
}
public void run() {
try{
final ServerSocket srvsocket = new ServerSocket(port);
log.info(String.format("Server started # %s\n", srvsocket));
while(!Thread.currentThread().isInterrupted()){
Socket newclient = srvsocket.accept();
// spawn thread and hand off new client to handler
new Thread(new ClientHandler(newclient)).start();
}
}
catch (Exception e) {
log.severe(e.getMessage());
}
log.info("server stopped");
}
// -------------------------------------------------
// client handler
// -------------------------------------------------
static class ClientHandler implements Runnable {
final Socket socket;
public ClientHandler(final Socket socket) {
assert socket != null : "serverthread is null";
this.socket = socket;
}
#SuppressWarnings("unused")
#Override final
public void run() {
log.info(String.format("new client # %s\n", socket.getRemoteSocketAddress()));
try {
final InputStream in = socket.getInputStream();
final OutputStream out = socket.getOutputStream();
// NOTE: this is just a stub busy loop!
for(;;) {
/* your protocol impl here .. */
}
} catch (Exception e) {
log.severe(e.getMessage());
}
finally {
try {
socket.close();
}
catch (Exception e) {
log.severe(e.getMessage());
}
}
}
}
}