i want to synchronize 2 Collections with each other. If something changes at the Server side, the connected Clients get updated.
I have a quite basic question. Do I now need to copy my java project and program the server in one and the client in the other one? But that sounds like quite a lot of unnecessary work. Can't I implement it all in one project an then start server and client in one main? Do I need threads? I'm kind of stuck what the best way is here.
Thanks in advance.
Because codereview doesn't allow my code cause it's not yet working, i post it now here in the hope, that you can help me.
public class Server implements Runnable{
private String hostName = "127.0.0.1";
private int portNumber;
private ServerSocket serverSocket;
private Socket clientSocket;
public Server(int port){
this.portNumber = port;
}
public void run(){
String line = "";
PrintWriter out = null;
BufferedReader in = null;
BufferedReader stdIn = null;
try{
this.serverSocket = new ServerSocket(this.portNumber);
}catch (IOException e) {
System.out.println("Could not listen on port");
}
try{
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.out.println("Accept failed");
}
try{
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
}catch (IOException e) {
System.out.println("Read failed");
}
while(true){
try{
line = in.readLine();
}catch (IOException e) {
System.out.println("Read failed");
}
System.out.println("line: "+line);
}
}
protected void finalize(){
//Objects created in run method are finalized when
//program terminates and thread exits
try{
serverSocket.close();
}catch (IOException e) {
System.out.println("Could not close socket");
}
}
}
public class Client implements Runnable{
private String hostName = "127.0.0.1";
private int portNumber = 6602;
private Socket clientSocket = null;
public Client(Socket client){
this.clientSocket = client;
}
public Client(int portNumber, String hostName){
this.portNumber = portNumber;
this.hostName = hostName;
}
public void run(){
String line;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader stdIn = null;
try{
if(clientSocket == null)
this.clientSocket = new Socket(this.hostName, this.portNumber);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
stdIn = new BufferedReader(new InputStreamReader(System.in));
out.println("Test string from client");
}catch (IOException e){
System.out.println("in or out failed");
}
}
}
public class DebugServerClient {
public static void testServerClient(){
int port = 6602;
Server srv = new Server(port);
Client clt = new Client(port, "127.0.0.1");
Thread s = new Thread(srv);
s.start();
Thread c = new Thread(clt);
c.start();
}
}
I changed it now to this and it seems to work. Is this a good way?
Related
the problem is this: I created a java Server using ServerSocket and Socket class. The html page is shown correctly but i have no idea on how can i send data (from an input type = text) to my java Server and then show the message i wrote on another html page.
The code is the following:
MyServer class:
private int port;
private ServerSocket ss;
private Socket s;
private PrintWriter pw;
public MyServer(){
port = 1245;
try{
ss = new ServerSocket(port);
System.out.println("server creato");
}
catch(Exception e){
e.printStackTrace();
}
listen();
}
public void listen(){
try{
while(true){
s = ss.accept();
System.out.println("OK");
if(s == null)
System.exit(1);
Client c = new Client(s);
c.start();
}
}catch(Exception e){
e.printStackTrace();
}
}
Client class (extends Thread):
public Client(Socket s){
this.s = s;
try{
pw = new PrintWriter(s.getOutputStream(), true);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
f = new File("test.html");
}catch(Exception e){
e.printStackTrace();
}
}
public BufferedReader getBr(){
return this.br;
}
public PrintWriter getPw(){
return this.pw;
}
public File getF(){
return this.f;
}
public void displayHtml(){
try {
FileInputStream fis = new FileInputStream(f);
BufferedReader br2 = new BufferedReader(new InputStreamReader(fis));
StringBuilder sb = new StringBuilder();
String ris = null;
while((ris = br2.readLine()) != null){
sb.append(ris);
//sb.append("\r\n");
}
pw.println("HTTP/1.1 200 OK");
pw.println("Content-Type: text/html");
pw.println("\r\n");
pw.println(sb.toString());
pw.flush();
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void
public void run(){
try{
displayHtml();
}catch(Exception e){
e.printStackTrace();
}
}
}
Finally, the Main class:
public static void main(String[] args){
MyServer ms = new MyServer();
}
I' m searching for a good solution. I wish not to delete all my code in order to do that. Thank you :)
This question already has an answer here:
no output for java client/server app [closed]
(1 answer)
Closed 5 years ago.
I've got a problem with my simple TCP/IP chat. It seems that my server doesn't receive messages from connected clients and I have no idea why it is happening.
Server code:
public class ChatServer {
public static final int MAX_CLIENTS = 10;
public static final ClientHandler[] clients = new ClientHandler[MAX_CLIENTS];
public void go(int port){
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Connection established on port "+port);
System.out.println("Waiting for clients...");
while (true){
Socket clientSocket = serverSocket.accept();
for (int i=0; i<clients.length;i++){
if (clients[i]==null){
ClientHandler clientHandler = new ClientHandler(clientSocket, clients);
clients[i] = clientHandler;
System.out.println("Added new client!");
clientHandler.start();
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ClientHandler class:
public class ClientHandler extends Thread {
private Socket socket;
private ClientHandler[] clients;
private PrintWriter out;
public ClientHandler(Socket clientSocket, ClientHandler[] clientsThreads){
socket = clientSocket;
clients = clientsThreads;
}
#Override
public void run() {
ClientHandler[] threads = this.clients;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
for (int i=0; i<threads.length;i++){
if (threads[i]!=null){
threads[i].out.println("***SERVER: New client entered the chat room!***");
}
}
while (true){
System.out.println("in while loop - reading and writing to the client socket");
String inputLine = in.readLine();
System.out.println(inputLine);
if (inputLine.startsWith("/quit")){
break;
}
for (int i=0; i<threads.length;i++){
if (threads[i]!=null){
threads[i].out.println(inputLine);
}
}
}
System.out.println("One of the clients is leaving the chat room");
for (int i=0; i<threads.length;i++){
if (threads[i]==this){
threads[i]=null;
}
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
And the client code:
public class ChatClient {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private BufferedReader stdLine;
private boolean closed = false;
public void go(String hostName, int port){
try {
initializeResource(hostName, port);
new Thread(new ServerReader()).start();
while (!closed){
out.println(stdLine.readLine().trim());
}
in.close();
out.close();
socket.close();
System.out.println("Goodbye!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to connect, please try again.");
}
}
public void initializeResource(String hostName, int port) throws IOException {
socket = new Socket(hostName, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
stdLine = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Connection established!");
}
public class ServerReader implements Runnable{
#Override
public void run() {
String inputLine = null;
try {
while ((inputLine=in.readLine())!=null){
System.out.println(inputLine);
if (inputLine.startsWith("Bye!")){
closed = true;
return;
}
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The result of running these applications for server:
Connection established on port 8000
Waiting for clients...
Added new client!
in while loop - reading and writing to the client socket
And for client:
Connection established!
***SERVER: New client entered the chat room!***
In the client version I can write messages in the terminal all the time, but none of these messages isn't received by the server (otherwise messages would be written in the server's terminal). I will appreciate any suggestion.
You need to flush after printing a line from the client:
while (!closed){
out.println(stdLine.readLine().trim());
out.flush();
}
I have a socket client sending text to a socket server but the ReadLine doesnt seem to wait to receive a line before proceeding. Here is the of the server receiving the text:
public void run() {
try {
serveurSocket = new ServerSocket(PORT_ID);
connexionSocket = serveurSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(connexionSocket.getInputStream()));
PrintWriter writer = new PrintWriter(connexionSocket.getOutputStream(), true);
messageRecu = "";
while (true) {
messageRecu = reader.readLine();
messageRecu = messageRecu.toUpperCase();
writer.println(messageRecu);
}
//reader.close();
//writer.close();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
After establishing the socket between client and server, the execution halts at reader.readLine until I send manually a string thru the socket. Which is normal and wanted. Codes resumes and its fine until its loops back to reader.ReadLine() where it will read a "null" line instead of waiting for input from the socket like it did the first time... this will obviously mess up the next command to uppercase. So how can I fix this?
EDIT: I'll add the client side if that can help understand.
public class ClientSocket {
private Socket clientSocket;
public boolean isClosed() { return clientSocket.isClosed(); }
public boolean connectToSocket (String ip, int port) {
try {
clientSocket = new Socket(ip, port);
return true;
}
catch (IOException e) {
System.out.println(e);
return false;
}
}
public String sendToServer(String messageClient) {
String messageRecu = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
writer.println(messageClient);
messageRecu = reader.readLine();
reader.close();
writer.close();
return messageRecu;
}
catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return messageRecu;
}
}
}
A button press will call "connectTosocket" to initiate the socket. A second button when pressed will send the content of a textfield using "sendToServer".
Server does receive the message and return it capitalized but I wish for the socket to remain open with the server and if I send an other string for the same sequence to happen. Not even sure it can be done :(
According to the documentation of BufferedReader#readLine, a null is returned if the end of stream has been reached.
Change your reading loop to :
while ((messageRecu = reader.readLine()) != null) {
messageRecu = messageRecu.toUpperCase();
writer.println(messageRecu);
}
//Get out of the loop when the end of stream is reached.
As per Reading from and Writing to a Socket chapter of the Java tutorial.
As a side note, while(true) loops are not really appreciated.
The "null" signals for end of connection from the client side - which is why the connection disconnects. If you want to support multiple requests, you should run a new ServerSocket.accept() each time and wait for a new client to connect.
KKMultiServer class:
import java.net.*;
import java.io.*;
public class KKMultiServer {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java KKMultiServer <port number>");
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
boolean listening = true;
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
while (listening) {
new KKMultiServerThread(serverSocket.accept()).start();
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
}
}
}
KKMultiServerThread class:
import java.net.*;
import java.io.*;
public class KKMultiServerThread extends Thread {
private Socket socket = null;
public KKMultiServerThread(Socket socket) {
super("KKMultiServerThread");
this.socket = socket;
}
public void run() {
try (
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
) {
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye"))
break;
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
You can read more about sockets in Oracle tutorials
I am writing a Server/Client chat where basically Multiple Clients connected to One Server. One client send a message to server Then all other Clients will get the same message. For example: Client A, B, C Connected to A same Server. Client A send Message To Server, Server then will send the same message to client B and C but exclude Client A.
I'm stuck at part where Server send out the message to all other clients.
Below is the code, I'm just a Java beginner so any help with the code will be much appreciate.
ServerSide
public class ServerP2P extends Thread{
private ServerSocket server = null;
private Socket clientSocket = null;
private ArrayList<ServerThread> clientThreadList = new ArrayList<>();
private int maxClient = 4;
private int port = 9990;
boolean listening = true;
public ServerP2P(){
try{
server = new ServerSocket(port);
}catch(IOException e){
e.printStackTrace();
return;
}
System.out.println("Server with Port "+port+" is Up and Running");
}
public void run(){
System.out.println("Room Chat Is Up");
while(listening){
for(int i = 0;i<clientThreadList.size();i++){
if(!clientThreadList.get(i).getConneection()){
System.out.println(clientThreadList.get(i)+" is removing from server because there is no conntection");
clientThreadList.remove(i);
}
}
try{
clientSocket = server.accept();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("User with IP "+clientSocket.getInetAddress()+" Has Connected to Server");
clientThreadList.add(new ServerThread(clientSocket));
try{
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
}
}
public ArrayList<ServerThread> listOFClient(){
return clientThreadList;
}
public static void main(String[] args){
ServerP2P server = new ServerP2P();
server.start();
}
}
ServerThread
public class ServerThread{
private Socket clientSocket;
private boolean connected;
private Incomming incommingData;
String msg = null;
private class Incomming extends Thread{
private DataInputStream input;
public void run(){
try{
input = new DataInputStream(clientSocket.getInputStream());
}catch(IOException e){
e.printStackTrace();
return;
}
System.out.println("User with IP "+clientSocket.getInetAddress()+" has connected");
while(true){
try{
Thread.sleep(200);
int msgSize = input.readInt();
byte[] msgByte = new byte[msgSize];
for(int i = 0; i < msgSize ; i++){
msgByte[i] = input.readByte();
}
msg = new String(msgByte);
System.out.println(msg);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public ServerThread(Socket newClientSocket){
this.clientSocket = newClientSocket;
connected = true;
incommingData = new Incomming();
incommingData.start();
}
public boolean getConneection(){
return connected;
}
public void closeConnection(){
try{
connected = false;
clientSocket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
ClientSide
public class ClientP2P{
private Socket serverSocket = null;;
private DataOutputStream output = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String[] args) {
ClientP2P client = new ClientP2P();
client.startConnect();;
}
public void startConnect(){
int port = 9990;
try {
serverSocket = new Socket("localhost", port);
System.out.println(serverSocket.isBound());
output = new DataOutputStream(serverSocket.getOutputStream());
System.out.println("Please Enter Your name: ");
String nameClient = reader.readLine();
output.writeInt(nameClient.length());
output.writeBytes(nameClient);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("You Are Connected");
System.out.println("Chat Can Start");
sendText();
}
public void sendText(){
try {
while (true) {
System.out.println("Type Message: ");
String msg = reader.readLine();
output.writeInt(msg.length());
output.writeBytes(msg);
System.out.println("Message sent");
recivedText();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void recivedText(){
try{
DataInputStream input = new DataInputStream(serverSocket.getInputStream());
int textSize = 0;
while(input.available() != 0){
byte[] byteString = new byte[textSize];
for(int i = 0; i < textSize;i++){
byteString[i] = input.readByte();
}
String txtServer = new String(byteString);
System.out.println(txtServer);
textSize = 0;
}
sendText();
}catch(Exception e){
e.printStackTrace();
}
}
}
Thanks For Your Time Guys.
Your ServerThread receives the messages and you want to send them to all other clients. One way you could achieve is to have the clients register themselves with the Server (this would help in the server not knowing the clients when it starts, which ideally should be the case). In your ServerThread, get a list of available clients from the server and loop through them and send the message to each one of them.
Use ObserverDesign pattern to hold the list of all your buddy/user to whom you wish to send message. Use HashMap to maintain a list of all the observer and its socket. Once the message is received, you retrieve the sockets of each user and write the same message on each socket.
I'm trying to write a little SocketServer and a fitting ClientApplet. The connection works (I echo out incoming/closing connections), but the server does not get any InputStream.
I just can't fix the problem and feel a bit lost :/
The complete project is here.
Here is the responsible part of my server:
MessageService.java
public class MessageService implements Runnable {
private final Socket client;
private final ServerSocket serverSocket;
MessageService(ServerSocket serverSocket, Socket client) {
this.client = client;
this.serverSocket = serverSocket;
}
#Override
public void run() {
PrintWriter out = null;
BufferedReader in = null;
String clientName = client.getInetAddress().toString();
try {
out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line;
System.out.println("Waiting for "+clientName);
/* HERE I TRY TO GET THE STREAM */
while((line = in.readLine()) != null) {
System.out.println(clientName + ": " + line);
out.println(line);
out.flush();
}
}
catch (IOException e) {
System.out.println("Server/MessageService: IOException");
}
finally {
if(!client.isClosed()) {
System.out.println("Server: Client disconnected");
try {
client.close();
}
catch (IOException e) {}
}
}
}
}
Part of Client
QueueOut.java
public class QueueOut extends Thread {
Socket socket;
public ConcurrentLinkedQueue<String> queue;
PrintWriter out;
public QueueOut(Socket socket) {
super();
this.socket = socket;
this.queue = new ConcurrentLinkedQueue<String>();
System.out.print("OutputQueue started");
}
#Override
public void start() {
try {
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("Running outputqueue");
while(true) {
if(this.queue.size() > 0) {
String message = this.queue.poll();
System.out.println("Sending "+message);
out.println(message+"\n");
}
}
}
catch (IOException ex) {
System.out.println("Outputqueue: IOException");
}
}
public synchronized void add(String msg) {
this.queue.add(msg);
}
}
I have reduced my post to the (as i think) necessary parts :)
Try getting your input stream before you get the output stream, even though you're not using it, you should match the inverse order on your client and your server (as discussed in another similar threads).
Edit:
Also see Socket programming
Good Luck!