I wanted to ask how to change following code, which needs USB connection and WIFI to work... (and I don't know why wifi...), to code, which needs only USB cable and NO WIFI!, because I don't want to be dependent on wifi...
Could you please help me? Some changes or additions in code? Thanks.
Code for Android:
private final Runnable connectToServer = new Thread()
{
#Override
public void run()
{
try
{// Get the server address from a dialog box.
String serverAddress = "192.168.0.23";
// Make connection and initialize streams
Socket socket = new Socket(serverAddress, 38300);
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Consume the initial welcoming messages from the server
for (int i = 0; i < 3; i++) {
System.out.println(in.readLine());
}
solveCube();
} catch (IOException e) {
e.printStackTrace();
}
}
};
private final Runnable initializeConnection = new Thread()
{
#Override
public void run()
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(generateCubeString());
out.println(generateCubeString());
String response ="";
try {
response = in.readLine();
if (response == null || response.equals("")) {
System.exit(0);
}
} catch (IOException ex) {
}
if (response.contains("Error")) {
} else {
solveCubeAnimate(response);
}
System.out.println(response);
final String finalResponse = response;
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(finalResponse);
}
});
}
};
Code for PC
private static class Capitalizer extends Thread {
private Socket socket;
private int clientNumber;
public Capitalizer(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client# " + clientNumber + " at " + socket);
}
/**
* Services this thread's client by first sending the
* client a welcome message then repeatedly reading strings
* and sending back the capitalized version of the string.
*/
public void run() {
try {
// Decorate the streams so we can send characters
// and not just bytes. Ensure output is flushed
// after every newline.
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Send a welcome message to the client.
out.println("Hello, you are client #" + clientNumber + ".");
out.println("Enter a line with only a period to quit\n");
// Get messages from the client, line by line; return them
// capitalized
while (true) {
String input = in.readLine();
if (input == null || input.equals(".")) {
break;
}
out.println(solveCube(input));
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
/**
* Logs a simple message. In this case we just write the
* message to the server applications standard output.
*/
private void log(String message) {
System.out.println(message);
}
}
private static class Connecter extends Thread {
/**
* Services this thread's client by first sending the
* client a welcome message then repeatedly reading strings
* and sending back the capitalized version of the string.
*/
public void run() {
try {
System.out.println("The capitalization server is running.");
int clientNumber = 0;
ServerSocket listener = new ServerSocket(38300);
try {
while (true) {
new Capitalizer(listener.accept(), clientNumber++).start();
}
} finally {
listener.close();
}
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Related
I made a Chat Application (Server/Client) using Java. Note: The server is ran as its own jar file and each client is ran as its own jar file.
Each client is on their own thread.
Whenever I send messages to the server, each client receives the message, however when I send messages from the client, only the server receives the message. When the client sends a message, I want all connected clients and the server to receive the message so all of the clients can communicate together and with the server as well.
I've looked at multiple posts and videos about this, but most were too confusing for me to understand.
Could someone please help me understand how I can send messages between threads? Thanks!
-- My Code --
Client:
public Client(User user, String address, int port) {
try {
socket = new Socket(address, port);
ClientApplicationUI app = new ClientApplicationUI();
app.setTitle("Chat Application - " + user.getUsername());
app.setVisible(true);
ServerConnection connection = new ServerConnection(socket, app);
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
new Thread(connection).start();
app.getButton().addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (app.getTextField().getText() != null && app.getTextField().getText().length() > 0) {
String message = MessageUtil.getMessage(Message.LOGGER_PREFIX) + " <" + user.getUsername() + "> " + app.getTextField().getText() + "\n";
try {
output.writeUTF(message);
output.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
} catch (UnknownHostException e) {
System.out.println(e);
System.out.println("Could not connect! Reason: " + e);
} catch (IOException e) {
System.out.println("Could not connect! Reason: " + e);
}
}
ServerConnection
public class ServerConnection implements Runnable {
#SuppressWarnings("unused")
private Socket socket;
private DataInputStream in;
private ClientApplicationUI app;
public ServerConnection(Socket socket, ClientApplicationUI app) throws IOException {
this.socket = socket;
this.app = app;
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
}
#Override
public void run() {
while (true) {
String message;
try {
message = in.readUTF();
app.logMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Server
public class Server {
private Socket socket = null;
private ServerSocket server = null;
private ExecutorService pool = Executors.newFixedThreadPool(4);
public Server (int port) {
try {
ApplicationUI app = new ApplicationUI();
app.setVisible(true);
server = new ServerSocket(port);
app.logMessage(MessageUtil.getMessage(Message.LOGGER_PREFIX) + " " + MessageUtil.getMessage(Message.INFO) + " Server started!\n");
app.logMessage(MessageUtil.getMessage(Message.LOGGER_PREFIX) + " " + MessageUtil.getMessage(Message.INFO) + " Waiting for new connections...\n");
while (true) {
socket = server.accept();
ConnectionHandler clientThread = new ConnectionHandler(socket, app);
app.logMessage(MessageUtil.getMessage(Message.LOGGER_PREFIX) + " " + MessageUtil.getMessage(Message.INFO) + " A new client has been accepted!\n");
pool.execute(clientThread);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server(58139);
}
}
ConnectionHandler
public class ConnectionHandler implements Runnable {
private Socket client;
private ApplicationUI app;
private DataInputStream in;
private DataOutputStream out;
public ConnectionHandler(Socket client, ApplicationUI app) throws IOException {
this.client = client;
this.app = app;
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
}
#Override
public void run() {
try {
app.getButton().addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (app.getTextField().getText() != null && app.getTextField().getText().length() > 0) {
String message = MessageUtil.getMessage(Message.LOGGER_PREFIX) + " <Server> " + app.getTextField().getText() + "\n";
try {
sendMessage(message);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
String message = "";
while (!message.equals("/stop")) {
message = in.readUTF();
app.logMessage(message);
}
} catch (IOException e) {
System.err.println("IO exception in connection handler!");
System.err.println(e.getStackTrace());
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendMessage(String message) throws IOException {
out.writeUTF(message);
out.flush();
}
}
You need to understand, how sockets work. They are always Client and Server.
There are two ways you could achieve what you want:
First solution:
Send the message which is meant for all clients to the server and let the server distribute the message to all the other clients. The server will need to keep track of the already connected clients, i.e. store their Socket.
Second solution: (which totally is not advisable)
If you want to send a message to a client of a network without haveing the actual server involved, you will need that client to act as a server, or the other way around. This means that every client will actually need to listen to every other client, instead of only the server.
You should definitely go with the first solution!
I have one client file clientRPC.java and server file serverRPC.java. Both communicate using TCP protocol and use objectinput and output stream to transfer data.
my client file:
public class clientRPC {
public static void main(String args[]) {
Socket s = null;
try {
int serverPort = 8888;
s = new Socket("localhost", serverPort);// server name is local host
//initializing input and output streams object and referencing them to get input and output
ObjectInputStream in = null;
ObjectOutputStream out = null;
out = new ObjectOutputStream(s.getOutputStream());
in = new ObjectInputStream(s.getInputStream());
MathsTutor mt = new MathsTutor();
out.writeObject(mt);
out.flush();
System.out.println("Welcome to Maths Tutor Service. The available maths exercises are:\n"
+ "Addition: Enter 'A' or 'a'\n"
+ "Subtraction: Enter 'S' or 's'\n"
+ "Multiplication: Enter 'M' or 'm'\n"
+ "Division: Enter 'D' or 'd'\n"
+ "Enter 'Q' or 'q' to quit");
//System.out.println();
MathsTutor mt1 = (MathsTutor) in.readObject();
String response = in.readUTF();
System.out.println(response);
} catch (UnknownHostException e) {
System.out.println("Socket:" + e.getMessage());
} catch (EOFException e) {
System.out.println("EOF:" + e.getMessage());
} catch (IOException e) {
System.out.println("readline:" + e.getMessage());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
System.out.println("close:" + e.getMessage());
}
}
}
}
}
and my server file :
public class serverRPC extends Thread {
String request;
String response;
public static void main(String args[]) {
try {
int serverPort = 8888;
ServerSocket listen_socket = new ServerSocket(serverPort);
while (true) {
Socket clientSocket = listen_socket.accept();
Connection c = new Connection(clientSocket);
}
} catch (IOException e) {
System.out.println("Listen socket:" + e.getMessage());
}
public serverRPC(String s) {
request = s;
}
}
class Connection extends Thread {
ObjectInputStream in;
ObjectOutputStream out;
Socket clientSocket;
public Connection(Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in = new ObjectInputStream(clientSocket.getInputStream());
out = new ObjectOutputStream(clientSocket.getOutputStream());
this.start();
} catch (IOException e) {
System.out.println("Connection:" + e.getMessage());
}
}
public void run() {
try {
MathsTutor mt = (MathsTutor) in.readObject();
InetAddress ip = clientSocket.getInetAddress();
System.out.println("The Received Message from Client at address:/" + ip.getHostAddress());
System.out.println("====================================");
MathsTutor mt1 = new MathsTutor();
out.writeObject(mt1);
while(true) {
// Read from input
String command = in.readUTF();
System.out.println(command);
}
//System.out.println();
} catch (EOFException e) {
System.out.println("EOF:" + e.getMessage());
} catch (IOException e) {
System.out.println("readline:" + e.getMessage());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {/*close failed*/
}
}
}
}
The problem is when I run server and then client on cmd, the client side displays the welcome msg and puts cursor on another line for user input but, I can't type anything, the cursor just blinks... I know this might be simple but it has taken already 3 hours for me and I'm stuck in the same thing.
The cursor marked with red keeps blinking but doesn't let me type anything.
You're writing an object with writeObject() and trying to read it with readUTF(). Illogical.
objects written with writeObject() must be read with readObject().
strings written with writeUTF() must be read with readUTF().
primitives written with writeXXX() must be read with readXXX(), for most values of X.
I'm trying to send multiple Objects through a socket to a java server.
To have a gerneral type I convert my messages into an instance of the class Message and send this object to the server.
I wrote a little testclass, which sends three objects to the server.
The problem is, only one objects reaches the server.
I tried nearly everything, without success.
My Server:
public class Server {
private ServerConfig conf = new ServerConfig();
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
Logger log = new Logger();
ServerSocket socket;
Chat chat = new Chat();
public static void main(String[] args) {
Server s = new Server();
if (s.runServer()) {
s.listenToClients();
}
}
public boolean runServer() {
try {
socket = new ServerSocket(port);
logToConsole("Server wurde gestartet!");
return true;
} catch (IOException e) {
logToConsole("Server konnte nicht gestartet werden!");
e.printStackTrace();
return false;
}
}
public void listenToClients() {
while (true) {
try {
Socket client = socket.accept();
ObjectOutputStream writer = new ObjectOutputStream(client.getOutputStream());
Thread clientThread = new Thread(new Handler(client, writer));
clientThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void logToConsole(String message) {
System.out.print(message);
}
public class Handler implements Runnable {
Socket client;
ObjectInputStream reader;
ObjectOutputStream writer;
User user;
public Handler(Socket client, ObjectOutputStream writer) {
try {
this.client = client;
this.writer = writer;
this.reader = new ObjectInputStream(client.getInputStream());
this.user = new User();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while (true) {
Message incomming;
try {
while ((incomming = (Message) reader.readUnshared()) != null) {
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
logToConsole(
"Vom Client: \n" + incomming.getType() + "-----" + incomming.getValue().toString());
handle(incomming);
}
} catch (SocketException se) {
se.printStackTrace();
Thread.currentThread().interrupt();
} catch (IOException ioe) {
ioe.printStackTrace();
Thread.currentThread().interrupt();
} catch (ClassNotFoundException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
private void handle(Message m) throws IOException {
String type = m.getType();
if (type.equals(config.ConstantList.Network.CHAT.toString())) {
chat.sendMessage(m);
} else if (type.equals(config.ConstantList.Network.LOGIN.toString())) {
System.out.println(user.login(m.getValue().get(0), writer));
System.out.println(m.getValue().get(0));
}
}
}
}
The Client:
public class Connect {
Socket client = null;
ObjectOutputStream writer = null;
ObjectInputStream reader = null;
private Config conf = new Config();
//private String host = conf.loadProp("ServerIP");
String host = "localhost";
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
public boolean connectToServer() {
try {
client = new Socket(host, port);
reader = new ObjectInputStream(client.getInputStream());
writer = new ObjectOutputStream(client.getOutputStream());
logMessages("Netzwerkverbindung hergestellt");
Thread t = new Thread(new MessagesFromServerListener());
t.start();
return true;
} catch (Exception e) {
logMessages("Netzwerkverbindung konnte nicht hergestellt werden");
e.printStackTrace();
return false;
}
}
public boolean isConnectionActive() {
if (client == null || writer == null || reader == null){
return false;
}else{
return true;
}
}
public void sendToServer(Message m) {
try {
writer.reset();
writer.writeUnshared(m);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
And I try to send the objects with the class:
public void sendChatMessage(String username, String message) throws InterruptedException {
ChatMessage cm = new ChatMessage();
cm.setChat(username, null, message);
Message m = new Message(cm);
conn.sendToServer(m);
System.out.println("SENDED");
}
public static void main(String[] args) throws InterruptedException {
String username = "testuser";
String chatmessage = "Hallo Welt!";
connect.connect();
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
}
I know that this is always the same message, but it is only for test purposes.
The messages are the objects they are Serializable and with only one object it works as designed.
Does anyone can see where I made my mistake?
while ((incomming = (Message) reader.readUnshared()) != null) {
Here you are reading an object, and blocking until it arrives.
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
Here you are reading another object, and blocking till it arrives, and then erroneously logging it as the object you already read in the previous line.
Instead of logging reader.readObject(), you should be logging the value of incoming, which you have also misspelt.
And the loop is incorrect. readObject() doesn't return null at end of stream: it throws EOFException. It can return null any time you write null, so using it as a loop termination condition is completely wrong. You should catch EOFException and break.
Found the solution, the line logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n"); in the Server class, blocks the connection.
I am trying to make an chatprogram in Java, when I send message on the client side the server side gets the message. But when I send from the server side to the client it do not get the message.
I cannot see what I am doing wrong.
The server side code:
private void serverStart(){
textArea.append("Starting server " + " \n");
try {
serverSocket = new ServerSocket(4444);
textArea.append("Waiting for Clients " + " \n");
//Reading message from the client
socket = serverSocket.accept();
textArea.append("Client Connected " + "\n");
//Send message to client
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true)
{
messageFromClient = in.readLine();
whileChat(messageFromClient);
}
} catch(IOException ioExecption) {
ioExecption.printStackTrace();
}
}
private void whileChat(String messageFromClient) {
showMessage(messageFromClient);
System.out.println("Message from client : " + messageFromClient);
}
protected static void showMessage(final String message) {
SwingUtilities.invokeLater(
new Runnable(){
public void run()
{
Gui.consoleTextArea.append(message + "\n");
}
});
}
public static void sendMessage(String message) {
out.println(message);
showMessage(name + " : " + message + "\n");
}
The Client side :
private void connectToServer() {
try {
socket = new Socket("localhost", 4444);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Thread clientThread = new Thread(new Runnable() {
#Override
public void run() {
try {
// attach to socket's output stream with auto flush turned on
//Send message to the server
out = new PrintWriter(socket.getOutputStream(),
true);
//Get return message from server
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
messageFromServer = in.readLine();
whileChatting(messageFromServer);
} catch (Exception e) {
e.printStackTrace();
}
}
});
clientThread.start();
}
private void whileChatting(String messageFromServer) {
showMessage(messageFromServer);
System.out.println("Message from server to client " + messageFromServer);
}
public static void Send(String msg) {
out.println(name + " : " + msg);
showMessage(name + " : " + msg + "\n");
}
protected static void showMessage(final String message) {
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
Gui.consoleTextArea.append(message);
}
});
}
Hope someone could help me with this problem.
You could perform a flush on the PrintWriter immediately after each println, or even better: Instance the PrintWriter with autoFlush=true:
out = new PrintWriter(socket.getOutputStream(), true);
In this way, each time you call println, printf, or format, the PrintWriter will perform a flush of the buffer at the end.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
This line is always waiting the client output, and your client also doesn't send message to server.
Attention: both your server and client not read user input.
I had implemented a Java Multithreading Socket Server. It's working fine and create a new thread for every new client connection. But I have a bug when the TCP connection is cutted by the client, the server enter in a infinit loop and i got this message for every loop:
ERROR : Bad Frame !!! : null
I tried to debug the execution and change the if else condition but always the same bug exist when a client disconnect.
Here is my server Code
public class TCPSockServer implements Runnable {
Socket sock;
static int counter = 0;
private static int TIMEOUT = 50000;
private static int MAX_TIMEOUT = 100000;
long lastReadTime;
public TCPSockServer(Socket sock) {
this.sock = sock;
}
public static void main(String[] args) {
try {
ServerSocket serverSock = new ServerSocket(12000);
System.out.println("TCPSockServer : Listening to PORT 12000 ...");
while (true) {
Socket newSock = serverSock.accept();
counter++;
InetAddress addr = newSock.getInetAddress();
System.out.println("TCPSockServer : Connection Number : "+ counter);
System.out.println("TCPSockServer : Connection made to "
+ addr.getHostName() + " : (" + addr.getHostAddress()
+ ")");
newSock.setSoTimeout(TIMEOUT);
newSock.setKeepAlive(true);
new Thread(new TCPSockServer(newSock)).start();
}
} catch (IOException e) {
System.err.println("Trackiz: Main : ERROR Connection Failed");
e.printStackTrace();
}
}
#Override
public void run() {
int clientID = counter;
try {
BufferedReader inStream = new BufferedReader(new InputStreamReader(
sock.getInputStream()));
StringBuilder inString = new StringBuilder();
String frame = null;
PrintStream outStream = new PrintStream(sock.getOutputStream());
while (true) {
if (inString.append((String) inStream.readLine()) == null) {
System.out.println("TCPSockServer : CLIENT NOT CONNECTED");
sock.close();
break;
} else {
lastReadTime = System.currentTimeMillis();
frame = inString.toString();
if (cond1(frame)) {
......
}else{
system.err.println("\nERROR : Bad Frame !!! : "+frame); // why it enter this else in an infinit loop
}
outStream.println(provt.sendCommand("TCPSockServer : ACK TO CLIENT"));
inString = null;
inString = new StringBuilder();
}
}
inStream.close();
outStream.close();
sock.close();
} catch (SocketTimeoutException e) {
if (!isConnectionAlive()) {
System.out.println("\nCONNECTION TERMINATED FROM CLIENT !"
+ clientID);
logger.log(Level.SEVERE, "TCPSockServer : Connection terminated with Client");
try {
sock.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
// TODO sendHeartBeat();
System.out.println("Sending HeartBeat ...");
}
e.printStackTrace();
} catch (IOException e) {
System.err.println("TCPSockServer : Connection Timeout. Try to reconnect !");
e.printStackTrace();
}
}
public boolean isConnectionAlive() {
return System.currentTimeMillis() - lastReadTime < MAX_TIMEOUT;
}
}
The append method will never return null, so this is not right, the if-branch will never be entered:
if (inString.append((String) inStream.readLine()) == null) {
The code should probably look more like this:
String line = inStream.readLine();
if (line == null) {
...
} else {
inString.append(line);
...
}