Good evening, I got this server and client here.
WebServer
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class WebServer {
public static void main(String[] args) throws Exception {
HttpServer http = HttpServer.create(new InetSocketAddress(8000), 0);
http.createContext("/test", new MyHandler());
http.setExecutor(null); // creates a default executor
http.start();
//NimServer nimserver = new NimServer(32778);
//nimserver.serve();
}
static class MyHandler implements HttpHandler {
//AtomicInteger atomicInteger = new AtomicInteger(0);
//int theValue = atomicInteger.get();
#Override
public void handle(final HttpExchange t) throws IOException {
final String response;
final String requestMethod = t.getRequestMethod();
if ("GET".equals(requestMethod)) {
// response = String.format("Besuche: %d%n", atomicInteger.addAndGet(1));
}
else if ("POST".equals(requestMethod)) {
// atomicInteger.set(0);
int clientno = new DataInputStream(t.getRequestBody()).readInt();
System.out.println("Send from Client: " + clientno);
int newclientno = clientno + 1;
System.out.println("Increased by Server: " + newclientno);
new DataOutputStream(t.getResponseBody()).writeInt(newclientno);
}
else {
throw new IOException("Unsupported method");
}
//t.sendResponseHeaders(200, response.length());
//final OutputStream os = t.getResponseBody();
//os.write(newclientno);
//os.close();
}
}
}
HttpClient
import java.net.*;
import java.io.*;
public class HttpClient {
public static int clientno = 0;
public static void main(String[] args) throws Exception {
//NimMessage clientnumber = new NimMessage();
//clientnumber.nachricht = "Client No: " + clientno;
URL test = new URL("http://localhost:8000/test");
HttpURLConnection connect = (HttpURLConnection) test.openConnection();
connect.setDoOutput(true);
connect.setDoInput(true);
connect.setRequestMethod("POST");
new DataOutputStream(connect.getOutputStream ()).writeInt(clientno);//send int out
int newclientno = new DataInputStream(connect.getInputStream()).readInt();
System.out.println("Send from Server: " + newclientno);
//BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
//String inputLine;
//System.out.println(clientnumber.createJsonNachricht().toString());
//while ((inputLine = in.readLine()) != null)
// System.out.println(inputLine);
//in.close();
}
}
I was able to send the integer clinetno from the client to the server and increase it at the server. But i can not figure out how to send the new integer newclientno back to the client and display it on the console. Any suggestions what i did wrong?
Okay i found my mistake, i had to add an header in the server to complete the connection. Which i already had but did not noticed it.
t.sendResponseHeaders(200, 0);
Related
I am learning Java networking and am trying to implement a simple UDP server-client program which exchanges messages between server and client. The program is working, but when I print the buffer, a lot of newlines get printed. What am I doing wrong? Here is my code for the server:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.net.SocketException;
class Q1Server {
private DatagramSocket ds;
private byte[] buff1, buff2;
private DatagramPacket dp1 = null, dp2 = null;
private InetAddress clientIP;
private int clientPort;
public Q1Server() throws SocketException{
ds = new DatagramSocket(1234);
buff1 = new byte[10000];
buff2 = null;
while(true) {
try {
dp1 = new DatagramPacket(buff1, buff1.length);
System.out.println("Waiting for connection...");
ds.receive(dp1);
System.out.print(new String(buff1, "UTF-8"));
buff1 = new byte[10000];
clientIP = dp1.getAddress();
clientPort = dp1.getPort();
System.out.println("Connected to IP: " + clientIP + " Port: " + clientPort);
buff2 = ("Thanks for connecting to the server!!!").getBytes(StandardCharsets.UTF_8);
dp2 = new DatagramPacket(buff2, buff2.length, clientIP, clientPort);
ds.send(dp2);
String cl = "";
while (!cl.equals("close")) {
dp1 = new DatagramPacket(buff1, buff1.length);
ds.receive(dp1);
System.out.println("here");
cl = new String(buff1, "UTF-8");
buff1 = new byte[10000];
System.out.println("Client: " + cl);
}
buff2 = ("Closing...\nGoodbye!!!").getBytes(StandardCharsets.UTF_8);
dp2 = new DatagramPacket(buff2, buff2.length, clientIP, clientPort);
ds.send(dp2);
} catch(IOException i) {
i.printStackTrace();
}
}
}
public static void main(String[] args) throws SocketException {
Q1Server server = new Q1Server();
}
}
And here is the code for client:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
class Q1Client {
private DatagramSocket ds;
private Scanner scanner;
private InetAddress ip;
private byte[] buff1, buff2;
private DatagramPacket dp1 = null, dp2 = null;
public Q1Client() throws SocketException, UnknownHostException {
ds = new DatagramSocket();
ip = InetAddress.getLocalHost();
buff1 = new byte[10000];
buff2 = null;
scanner = new Scanner(System.in);
try {
buff2 = ("Start").getBytes(StandardCharsets.UTF_8);
dp2 = new DatagramPacket(buff2, buff2.length, ip, 1234);
ds.send(dp2);
dp1 = new DatagramPacket(buff1, buff1.length);
ds.receive(dp1);
System.out.println(new String(buff1, "UTF-8"));
buff1 = new byte[10000];
String msg = "";
while(!msg.equals("close")) {
System.out.println("here");
msg = scanner.nextLine();
System.out.println("Typed: " + msg);
buff2 = msg.getBytes(StandardCharsets.UTF_8);
dp2 = new DatagramPacket(buff2, buff2.length, ip, 1234);
ds.send(dp2);
}
dp1 = new DatagramPacket(buff1, buff1.length);
ds.receive(dp1);
System.out.println(new String(buff1, "UTF-8"));
} catch(IOException i) {
i.printStackTrace();
}
}
public static void main(String[] args) throws SocketException, UnknownHostException{
Q1Client client = new Q1Client();
}
}
I think the problem may be due to the size of the buffer. The byte buffer array is pre-initialized with the size that large than the actual text being sent. But what is the other way to do it? How do I know the size of the buffer dynamically?
Here are my 2 different programs that connect to each other:
SERVER
package authenticateddns;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class Server {
public static final int PORT = 4444;
int clientNumber;
static String[][] url = new String[8][2];
public static void main(String args[]) throws IOException {
url[0][0] = "www.google.com";
url[0][1] = "172.217.11.174";
url[1][0] = "www.facebook.com";
url[1][1] = "31.13.77.36";
url[2][0] = "www.youtube.com";
url[2][1] = "74.125.65.91";
url[3][0] = "www.wikipedia.org";
url[3][1] = "91.198.174.192";
url[4][0] = "www.twitter.com";
url[4][1] = "199.59.149.230";
url[5][0] = "www.amazon.com";
url[5][1] = "72.21.211.176";
url[6][0] = "www.yahoo.com";
url[6][1] = "98.137.149.56";
url[7][0] = "www.abc.com";
url[7][1] = "199.181.132.250";
new Server().runServer();
}
public void runServer() throws IOException {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server up and ready for connection...");
while (true) {
ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
Socket socket = serverSocket.accept();
pool.submit(() -> new ServerThread(socket, clientNumber, url).start());
System.out.println("New client has joined");
clientNumber++;
}
}
}
SERVERTHREAD
package authenticateddns;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread extends Thread {
Socket socket;
String clientName;
int clientN;
String url[][];
byte[] message = new byte[4];
ServerThread(Socket socket, int clientNumber, String[][] url)
{
this.socket = socket;
clientN = clientNumber;
this.url=url;
message[0]=1;
message[1]=2;
message[2]=3;
message[3]=4;
}
public void run()
{
try
{
String userURL;
String ip = "";
clientName = "Client" + clientN;
//this is from the server
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//this is from the client
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
//dout.writeInt(message.length);
//dout.write(message);
//this reads in from client
while(true)
{
if((userURL = bufferedReader.readLine()) != null)
{
userURL = userURL.replace("\n", "").replace("\r", "");
for(int i = 0; i < 8; i++)
{
if(url[i][0].equals(userURL))
{
ip = url[i][1];
break;
}
if(!url[i][0].equals(userURL)&&i==7)
{
ip = "IP Address not found";
}
}
//System.out.println(clientName);
System.out.println(" " + clientName + ": Desired URL: " + userURL);
System.out.println(" " + clientName + ": Sending IP: " + ip);
System.out.println("");
//send this to client
printWriter.println("Server Recieved Message");
printWriter.println(ip);
dout.write(message);
System.out.println("worked");
}
//socket.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
AND THEN YOU HAVE THE CLIENT PROGRAM
package client;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.InetAddress;
public class Client {
public static void main(String[] args) throws IOException
{
//InetAddress localIP;
//localIP = InetAddress.getLocalHost();
//Socket socket = new Socket("10.0.36.89", 4444);
Socket socket = new Socket("10.1.43.10", 4444);
String ip;
String confirm;
int length;
// from client
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
BufferedReader buffedReader = new java.io.BufferedReader(new InputStreamReader(System.in));
//from server
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
DataInputStream din = new DataInputStream(socket.getInputStream());
while(true)
{
//this sends input
System.out.print("Enter Destired URL: ");
String readerInput = buffedReader.readLine();
readerInput = readerInput.replace("Enter Destired URL: ", "");
printWriter.println(readerInput);
if((confirm = bufferedReader.readLine()) != null)
{
System.out.println(confirm);
}
//this reads in from server
if((ip = bufferedReader.readLine()) != null)
{
System.out.println("Server returns IP Address: " + ip);
System.out.println("");
}
int j = 4;
byte[] message = new byte[4];
if(j>0)
{
din.readFully(message, 0, 4); // read the message
j=j-1;
}
for(int i = 0; i < 4; i++)
{
System.out.print(message[i]);
}
}
}
}
The issue is that the client is not receiving the byte array. It only works if you run the Server program in debugger and put a breakpoint on where it writes to Client.
Does anyone have any ideas?
This is my server code:
package ServerSideHammingCodeCheckingAndResponse;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private ServerSocket serverSocket;
private int port;
public Server(int port)
{
this.port = port;
}
public void start() throws IOException
{
System.out.println("Server starts at port:" + port);
serverSocket = new ServerSocket(port);
System.out.println("Waiting for client...");
Socket client = serverSocket.accept();
sendMessage(client, "This is Hamming Code Checking.");
boolean checkInput = false;
String input = null;
while (!checkInput)
{
input = getMessage(client);
if(input.length() == 7 && input.matches("[01]+"))
checkInput = true;
else
sendMessage(client, "invalid");
}
sendMessage(client, input);
}
private void sendMessage(Socket client, String message) throws IOException
{
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
writer.write(message);
writer.flush();
writer.close();
}
private String getMessage(Socket client) throws IOException
{
String userInput;
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
userInput = reader.readLine();
return userInput;
}
public static void main(String[] args)
{
int portNumber = 9987;
try {
Server socketServer = new Server(portNumber);
socketServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is my client code:
package ClientSideDataTransmitter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client
{
private String hostname;
private int port;
Socket socketClient;
public Client(String hostname, int port)
{
this.hostname = hostname;
this.port = port;
}
public void connect() throws UnknownHostException, IOException
{
System.out.println("Attempting to connect to " + hostname + ":" + port);
socketClient = new Socket(hostname, port);
System.out.println("\nConnection Established.");
}
public void readResponse() throws IOException
{
String userInput;
BufferedReader reader = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
System.out.print("Response from server: ");
while ((userInput = reader.readLine()) != null) {
System.out.println(userInput);
}
}
public void sendData() throws IOException
{
Scanner sc = new Scanner(System.in);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socketClient.getOutputStream()));
System.out.println("Enter a 7-bits binary as message to server:\n");
String input = sc.nextLine();
writer.write(input);
writer.flush();
}
public static void main(String arg[])
{
Client client = new Client ("localhost", 9987);
try {
client.connect();
client.readResponse();
client.sendData();
client.readResponse();
} catch (UnknownHostException e) {
System.err.println("Host unknown. Cannot establish connection");
} catch (IOException e) {
System.err.println("Cannot establish connection. Server may not be up." + e.getMessage());
}
}
}
I havent finish up the code so please ignore minor mistakes in the code.
When I start Server, then Client, and send an input from Client to Server. Server seems not getting the data from Client since I send back that input from server to client to print it out, it prints nothing.
I think there may be problems in the method getMessage() in the server code but I cant fix it. Please help me fix the code. Many thanks!
Server:
Server starting at port 9987
Waiting for client...
Client:
Attempting to connect to localhost:9987
Connection Established.
Response from server: This is Hamming Code Checking.
Enter a 7-bits binary as message to server:
1234567
Response from server:
On top of Shriram suggestions I advise you to use PrintWriter here to avoid closing connection. It's also somewhat more convenient to use. Here's working example:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket serverSocket;
private int port;
public Server(int port) {
this.port = port;
}
public void start() throws IOException {
System.out.println("Server starts at port:" + port);
serverSocket = new ServerSocket(port);
System.out.println("Waiting for client...");
Socket client = serverSocket.accept();
sendMessage(client, "This is Hamming Code Checking.");
boolean checkInput = false;
String input = null;
while (!checkInput) {
input = getMessage(client);
if (input.length() == 7 && input.matches("[01]+"))
checkInput = true;
else
sendMessage(client, "invalid");
}
sendMessage(client, input);
}
private void sendMessage(Socket client, String message) throws IOException {
PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
writer.println(message);
}
private String getMessage(Socket client) throws IOException {
String userInput;
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
userInput = reader.readLine();
return userInput;
}
public static void main(String[] args) {
int portNumber = 9987;
try {
Server socketServer = new Server(portNumber);
socketServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private String hostname;
private int port;
Socket socketClient;
public Client(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}
public void connect() throws UnknownHostException, IOException {
System.out.println("Attempting to connect to " + hostname + ":" + port);
socketClient = new Socket(hostname, port);
System.out.println("\nConnection Established.");
}
public void readResponse() throws IOException {
String userInput;
BufferedReader reader = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
System.out.print("Response from server: ");
userInput = reader.readLine();
System.out.println(userInput);
}
public void sendData() throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter writer = new PrintWriter(socketClient.getOutputStream(), true);
System.out.println("Enter a 7-bits binary as message to server:\n");
String input = sc.nextLine();
writer.println(input);
}
public static void main(String arg[]) {
Client client = new Client("localhost", 9987);
try {
client.connect();
client.readResponse();
client.sendData();
client.readResponse();
} catch (UnknownHostException e) {
System.err.println("Host unknown. Cannot establish connection");
} catch (IOException e) {
System.err.println("Cannot establish connection. Server may not be up." + e.getMessage());
}
}
}
The problem with your code is writer.close() in sendMessage() will internally close the writer objects. Your server program will be closed and no longer in connection to accept the connections.
You're losing data by creating a new BufferedReader per message. You need to use the same one for the life of the socket. Ditto 'PrintWriter` or whatever you use to send.
Here's an oversimplified server-client connection in Java that doesn't seem to work. I've made a server that serves properly to a browser. I've also made a http connection that receives data fine from an internet site. However, getting the two to talk to each other seems to be difficult.
I have three classes. Common contains strings and data for the Server and Client classes to use.
Common.java:
import java.io.Serializable;
public class Common {
public static class Data implements Serializable{
private static final long serialVersionUID = 1L;
public String value = null;
public Data(String value) {
this.value = value;
}
}
public static final String PROTOCOL = "http";
public static final String HOST = "localhost";
public static final Integer PORT = 39640;
public static final String PAGE = "/test";
public static final String POST = "POST";
}
Server.java:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.*;
#SuppressWarnings("restriction")
public class Server {
public static class Handler implements HttpHandler {
#Override
public void handle(HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().add("accept", "*/*");
try {
ObjectInputStream in = new ObjectInputStream(exchange.getRequestBody());
Common.Data data = (Common.Data) in.readObject();
System.out.println(data.value);
exchange.sendResponseHeaders(200, 0);
exchange.close();
} catch(Exception e) {
exchange.sendResponseHeaders(404, 0);
exchange.close();
}
}
}
public static void main(String[] args) {
InetSocketAddress socketAddress = null;
HttpServer server = null;
try {
InetAddress address = InetAddress.getByName(Common.HOST);
socketAddress = new InetSocketAddress(address, Common.PORT);
server = HttpServer.create(socketAddress, 10);
//Add contexts
server.createContext(Common.PAGE, new Handler());
server.start();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Client.java
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Client {
public static void main(String[] args) {
try {
URL url = new URL(Common.PROTOCOL, Common.HOST, Common.PORT, Common.PAGE);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(Common.POST);
connection.setDoOutput(true);
connection.connect();
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject(new Common.Data("Hello world"));
} catch(Exception e) {
e.printStackTrace();
}
}
}
EDIT
I used ss -nt to determine that a connection is in some way occurring, even though no data is being transferred.
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 10.0.2.15:46759 108.168.151.6:80
ESTAB 0 0 10.0.2.15:59918 198.252.206.149:443
ESTAB 0 0 127.0.0.1:39030 127.0.0.1:52906
ESTAB 0 0 ::ffff:127.0.0.1:39640 ::ffff:127.0.0.1:35764
ESTAB 0 0 ::ffff:127.0.0.1:35764 ::ffff:127.0.0.1:39640
ESTAB 0 0 ::ffff:127.0.0.1:52906 ::ffff:127.0.0.1:39030
I´ve changed the following points and it works, in a laptop with Windows Vista and JDK 1.6.0_10 :
Commons:
public static final String HOST = "127.0.0.1";
//I´ve changed it to the localhost ip (windows)
Server, in the handle method, writing some chars to output and closing the exchange:
public void handle(final HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().add("accept", "*/*");
try {
final ObjectInputStream in = new ObjectInputStream(exchange.getRequestBody());
final Common.Data data = (Common.Data) in.readObject();
System.out.println(data.value);
exchange.sendResponseHeaders(200, 0);
exchange.getResponseBody().write("Hello!".getBytes());
} catch (final Exception e) {
exchange.sendResponseHeaders(404, 0);
} finally {
exchange.close();
}
In Client class, I read the InputStream from response and print it:
public static void main(final String[] args) {
try {
final URL url = new URL(Common.PROTOCOL, Common.HOST, Common.PORT, Common.PAGE);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(Common.POST);
connection.setDoOutput(true);
connection.connect();
final ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject(new Common.Data("Hello world"));
out.close();
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
connection.disconnect();
} catch (final Exception e) {
e.printStackTrace();
}
}
Nothing happens unless you get the input stream, the error stream, or the response code.
This is because the output is buffered until you do so, so that the Content-length header can be set accurately.
You can bypass that by using chunked or fixed-length transfer mode.
Server and client communicating with my own protocol which looks like XMPP. I should to realize chat application. So when one user write String it immedeatly should be sended to other client through the server. I have method sendToAll on server. But user see the message of other user only when it press enter.
How can user receive messages without pressing enter button?
So this is my client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.apache.log4j.Logger;
import dataart.practice.protocols.XMLProtocol;
public class Client {
public static final String SERVER_HOST = "localhost";
public static final Integer SERVER_PORT = 4444;
public static final Logger LOG = Logger.getLogger(Client.class);
private static BufferedReader in;
private static PrintWriter out;
private static BufferedReader inu;
public static void main(String[] args) throws IOException {
System.out.println("Welcome to Client side");
XMLProtocol protocol = new XMLProtocol();
Socket fromserver = null;
fromserver = new Socket(SERVER_HOST, SERVER_PORT);
in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));
out = new PrintWriter(fromserver.getOutputStream(), true);
inu = new BufferedReader(new InputStreamReader(System.in));
String fuser, fserver;
while (true){
if(in.ready()){//fserver = in.readLine()) != null) {
System.out.println("asdasdsd");
fuser = inu.readLine();
if (fuser != null) {
if (fuser.equalsIgnoreCase("close"))
break;
if (fuser.equalsIgnoreCase("exit"))
break;
protocol.setComId((long) 0);
protocol.setContent(fuser);
protocol.setLogin("Guest");
try {
JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
jaxbMarshaller.marshal(protocol, out);
out.flush();
} catch (JAXBException e) {
LOG.error("Error while processing protocol" + e);
}
}
}
}
out.close();
in.close();
inu.close();
fromserver.close();
}
}
And Server with ServerThread.
public static void main(String[] args) throws IOException {
LOG.trace("Server started");
ServerSocket s = new ServerSocket(SERVER_PORT);
try {
while (true) {
LOG.trace("Waiting for connections...");
Socket socket = s.accept();
try {
// new ServerThread(socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
userCounter++;
addUser("Guest" + userCounter, out);
LOG.trace("User " + userCounter + " has been added!");
exec.execute(new ServerThread(socket, in, out));
} catch (IOException e) {
socket.close();
}
}
} finally {
s.close();
}
}
ServerThread.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.Socket;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import org.apache.log4j.Logger;
import dataart.practice.protocols.XMLProtocol;
import dataart.practice.serverUtils.Commands;
public class ServerThread implements Runnable {
private static final Logger LOG = Logger.getLogger(ServerThread.class);
private XMLProtocol protocol;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private String buffer = "";// may be exist another. way but it's not working
private Boolean login = false;
public ServerThread(Socket s, BufferedReader in, PrintWriter out) throws IOException {
this.in = in;
this.out = out;
out.println("</XMLProtocol>");
socket = s;
new Thread(this);
}
public void run() {
try {
while (true) {
if ((buffer = in.readLine()) != null) {
if (buffer.endsWith("</XMLProtocol>")) {
protocol = getProtocol(buffer);
//Server.onlineUserList.put(protocol.getLogin(), out);
/* if (!login){
out.println("Maybe login first?");
}
*/
LOG.trace("Getting message from user: " + protocol.getLogin() + " recived message: " + protocol.getContent());
///out.println(protocol.getLogin() + " says:" + protocol.getContent());
Server.sendToAll(protocol.getContent()+"</XMLProtocol>");
} else {
LOG.trace("Nop protocol do not send with it end");
}
}
}
} catch (IOException e) {
LOG.error("Error in reading from stream: " + e);
} catch (JAXBException e) {
LOG.error("Error in Marshalling: " + e);
} finally {
try {
socket.close();
LOG.trace("Socket closed");
} catch (IOException e) {
LOG.error("Socket no closed" + e);
}
}
}
public XMLProtocol getProtocol(String buffer) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (XMLProtocol) jaxbUnmarshaller.unmarshal(new StreamSource(new StringReader(buffer)));
}
public Boolean loginIn(XMLProtocol protocol) {
return true;
}
}
You will need to multi-thread both the client and server. The client will need one thread that listens for messages from the server and writes them to his/her screen and one thread that waits for his/her keyboard input and sends it to the server. Likewise for each connection to the server, it will need a thread waiting for input from the client and one thread sending output from other users to the client.
The reason you don't see incoming messages until you press enter is because of the client while loop. It's commented out now, but it looks like your loop used to:
- Read incoming messages from server
- Read input from keyboard
- Send input to server
So you read whatever was available from the server, and then the client waits for more keyboard input before reading from the server again (in the next iteration).
Another word of advice, from my understanding, creating JAXBContext can be an expensive operation. You don't need to recreate it every time you send a message. Consider initializing one in your server and client and then reusing it for each marshall/unmarshall.
Try this,
Do Not use BufferedReader() with PrintWriter..... PrintWriter is itself the Bridge between byte level socket data and character form.
Eg:
I am showing for a single client, use the while loop for n nos of clients
ServerSocket s = new ServerSocket(4444);
Socket incoming = s.accept();
OutputStream output = s.getOutputStream();
PrintWriter pw = new PrintWriter(output,true);
System.out.println(pw.write(new Scanner(System.in).nextLine()));