Client Server exchanged message in order - java

Below are my server and client authentication program. I was wondering am I doing it in the correct way, I want the message being send and receive in order which mean, the server only send the data after receiving the input from client, same thing goes to client, after receiving the response from server then replying.
Client class:
try {
// Set up the socket
connection = new Socket("127.0.0.1", 55555);
PrintWriter out = new PrintWriter(connection.getOutputStream(),
true);
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
// Client --> Server 'A'
BigInteger A = client.Step1(username, password);
out.println(A);
// Client received s , B
byte[] s = new BigInteger(in.readLine()).toByteArray();
BigInteger B = new BigInteger(in.readLine());
// Client --> Server 'A'
BigInteger M1 = client.Step2(new BigInteger(s), B);
out.println(M1);
// Client received M2
BigInteger M2 = new BigInteger(in.readLine());
boolean clientVerify = client.clientVerify(M2);
if (clientVerify) {
System.out.println("Client OK");
} else {
System.out.println("NOT OK");
}
out.close();
in.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
Server class:
try {
socServer = new ServerSocket(55555);
System.out.println("Waiting Client....");
connection = socServer.accept();
System.out.println("Client Connected");
PrintWriter out = new PrintWriter(connection.getOutputStream(),
true);
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
// Server received A
BigInteger A = new BigInteger(in.readLine());
// Server --> Client 's' , 'B'
out.println(new BigInteger(s));
BigInteger B = server.step1(username, new BigInteger(s), v);
out.println(B);
server.step2(A);
// Server received 'M1'
BigInteger M1 = new BigInteger(in.readLine());
boolean serverVerify = server.serverVerify(M1);
if (serverVerify) {
// Server --> Client 'M2'
BigInteger M2 = server.step2(A, M1);
out.println(M2);
System.out.println("Server OK");
} else {
System.out.println("NOT OK");
connection.close();
}
out.close();
in.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
Am I able to capture the data through Wireshark?

Related

How to receive multiple POST requests from same client on the server

I am trying to write a java server and client such that the client sends the server a POST request with some xml string read from a file as payload, the server fetches the request from the client and sends it an acknowledgment, and this process repeats till all the xml strings that the client has have been sent (as separate POST requests).
I have made the code for 1 single exchange of request and response between the client and server. But I am unable to extend it for multiple requests from the same client because the client waits for server's response and the server's response is not sent to the client till I write wr.close() or socket.close() after writing bytes in the DataOutputStream object. But as soon as I write either of the two commands, my connection between the server and client closes and the client needs to establish connection all over again in order to send the second request.
This is my server side function that receives the request and sends the response:
public HTTPServer(int port) {
try {
private ServerSocket server = new ServerSocket(port);
private Socket socket = server.accept();
int ind = 0;
while (ind<19) {
//socket is an instance of Socket
InputStream is = socket.getInputStream();
InputStreamReader isReader = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isReader);
//code to read and print headers
String headerLine = null;
while ((headerLine = br.readLine()).length() != 0) {
System.out.println(headerLine);
}
ind++;
//send response to the client
Date today = new Date();
String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today;
DataOutputStream wr = new DataOutputStream(socket.getOutputStream());
wr.writeBytes(httpResponse);
wr.flush();
}
socket.close(); // or wr.close()
} catch (IOException i) {
System.out.println(i);
}
}
This is my client side code:
public void postMessage() throws IOException {
// string to read message from input
File folder = new File("path/to/my/files");
String[] listOfFiles = folder.list();
for (int i = 0; i < listOfFiles.length; i++) {
File file = new File(listOfFiles[i]);
Scanner sc = null;
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final URL url = new URL("http://localhost:8080");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setDoOutput(true);
conn.setConnectTimeout(5000);
// Send post request
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
String line = "";
while (sc.hasNextLine()) {
line += sc.nextLine();
line += '\n';
}
wr.writeBytes(line);
wr.flush();
wr.close();
// read response
BufferedReader in;
if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
}
}
Unless I write socket.close() or wr.close() after wr.flush(), my response is not sent to the client and the client keeps waiting for it, but as soon as I do, the server socket is closed and the code terminates. How can I send response to my client without having to close the socket?
EDIT:
This is updated HTTP Client code, which uses sockets to send HTTP request, but the error persists.
public static void main(String[] args) throws Exception {
InetAddress addr = InetAddress.getByName("localhost");
Socket socket = new Socket(addr, 8080);
boolean autoflush = true;
// string to read message from input
File folder = new File("/Users/prachisingh/IdeaProjects/requests_responses");
String[] listOfFiles = folder.list();
for (int i = 0; i < listOfFiles.length; i++) {
System.out.println("File " + listOfFiles[i]);
File file = new File("/Users/prachisingh/IdeaProjects/requests_responses/" + listOfFiles[i]);
Scanner sc = null;
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line = "";
while (sc.hasNextLine()) {
line += sc.nextLine();
line += '\n';
}
PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// send an HTTP request to the web server
out.println("POST / HTTP/1.1");
out.println("Host: http://localhost:8080");
out.println(line);
out.println();
// read the response
boolean loop = true;
StringBuilder sb = new StringBuilder(8096);
while (loop) {
if (in.ready()) {
int r = 0;
while (r != -1) {
r = in.read();
sb.append((char) r);
}
loop = false;
}
}
System.out.println(sb.toString());
}
socket.close();
}

How can I read from java socket?

I'm trying to send and read the reply from the socket with no luck. What I got so far is, connected to the server and read connection reply (header from Ericsson telco exchange) and printed it on System.out. What I need to be able to do is send a command to the exchange and get the reply and that's where I'm stuck. Below is my test code.
Any help is appreciated.
public class ExchangeClientSocket {
public void run() {
try {
int serverPort = 23;
InetAddress host = InetAddress.getByName("my.host.ip.address");
Socket socket = new Socket(host, serverPort);
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(isr);
int i;
StringBuffer buffer = new StringBuffer();
while (true) {
i = in.read();
if (i == 60) {
break;
} else {
buffer.append((char) i);
}
}
// this returns the head from exchange
System.out.println(buffer.toString());
out.write("allip;"); // command I want to send to exchange
out.flush();
System.out.println(in.ready()); // this returns false
System.out.println(in.read());
System.out.println("damn..");
buffer = new StringBuffer();
// can't get into this loop
// this is where I want to read the allip response
while ((i = in.read()) != -1) {
i = in.read();
if (i == 60) {
break;
} else {
buffer.append((char) i);
}
}
System.out.println(buffer.toString());
out.close();
in.close();
socket.close();
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is a suggestion on how to get input from the server after writing to the output stream: simply get another input stream from the socket.
So instead of reading from the same stream you create a new stream from the socket after you sent the command and read it.
I would therefore suggest to change your code to this here:
public void run() {
try {
int serverPort = 23;
InetAddress host = InetAddress.getByName("my.host.ip.address");
Socket socket = new Socket(host, serverPort);
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(isr);
int i;
StringBuffer buffer = new StringBuffer();
while (true) {
i = in.read();
if (i == 60) {
break;
} else {
buffer.append((char) i);
}
}
// this returns the head from exchange
System.out.println(buffer.toString());
out.write("allip;"); // command I want to send to exchange
out.flush();
// Create a new input stream here !!!
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
buffer = new StringBuffer();
// can't get into this loop
while ((i = in.read()) != -1) {
i = in.read();
if (i == 60) {
break;
} else {
buffer.append((char) i);
}
}
System.out.println(buffer.toString());
out.close();
in.close();
socket.close();
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Passing multiple objects from children to main thread

i've this problem, I've a Multithreaded server running. here it's the code:
ServerSocket serverSocket=null; // defining a server socket to listen data
Socket clientSocket = null; // defining a client socket to send data
final int port=8080;
int i=0;
try {
serverSocket = new ServerSocket(port); // Opening a server socket to listen for client calls
System.out.println("Server started.");
} catch (Exception e) {
System.err.println("Port already in use.");
System.exit(1);
}
while (true) {
try {
clientSocket = serverSocket.accept(); //binding server socket to client socket incoming call and accepting call
System.out.println("Accepted connection : " + clientSocket);
i=i+1;
Thread t = new Thread(new newClientHandler(clientSocket, NodePRs[1]),"thread"+i); //Create a new thread to handle the single call coming from one client
System.out.println("Thread "+t.getName()+" is starting");
t.start(); //Starting the run method contained in newCLIENTHandler class
} catch (Exception e) {
System.err.println("Error in connection attempt.");
}
}//end while
All that i need is to let the children thread (opened every time a client request come) pass (like a function return) 4 variables when the children thread die. The newClientHandler code is this:
public class newClientHandler implements Runnable {
private final static int FILE_SIZE=6022386;
private Socket clientSocket;
private PaillierPrivateKey PrivKey;
ServerSocket servSock;
BigInteger[] msg = null;
BigInteger preamble = null;
int bytesRead;
int current = 0;
DataOutputStream dos = null;
BufferedReader dis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
int msgtype=-1;
int num_of_rx_cnks=-1;
public newClientHandler(Socket client, PaillierPrivateKey PR) {
this.clientSocket = client;
this.PrivKey = PR;
}
//I CAN RECEIVE 3 TYPES OF MESSAGES: SHARE, THE ENCRYPTED PASSWORD, THE 4 PDMS
public void run() {
try{
ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
preamble = (BigInteger) ois.readObject();
System.out.println("Received Preamble is:"+preamble);
oos.writeObject("Received preamble");
msg =(BigInteger[]) ois.readObject();
System.out.println("Received Message is:"+msg+"\n"+msg[0]+"\n"+msg[2]);
String sPlain = Utilities.bigIntegerToString(preamble);
String[] splitArr=Pattern.compile("-").split(sPlain);
msgtype=Integer.parseInt(splitArr[0]);
num_of_rx_cnks=Integer.parseInt(splitArr[1]);
System.out.println("Message type: "+msgtype+"\n"+"Number of received cnks: "+num_of_rx_cnks);
//a questo punto ho i miei 29 biginteger. Li devo sistemare uno accanto all'altro e rimettere nel file.
switch(msgtype){
case 1: //Share received
System.out.println("Received the share");
for(int i=0;i<num_of_rx_cnks;i++){
String name = new String();
if(i<9){
name="Cyph2"+".00"+(i+1);
}
if(i>8){
name="Cyph2"+".0"+(i+1);
}
Utilities.newBigIntegerToFile(msg[i], name);
}
Utilities.retrieveShare(PrivKey, 2,"myShare");
int l, w;
BigInteger v, n, shares, combineSharesConstant;
BigInteger[] viarray=new BigInteger[5];
PaillierPrivateThresholdKey[] res = null;
try {
FileReader File= new FileReader("myShare");
BufferedReader buf=new BufferedReader(File);
String line=buf.readLine();
l = Integer.parseInt(line.split(":")[1]);
line = buf.readLine();
w = Integer.parseInt(line.split(":")[1]);
line = buf.readLine();
v = new BigInteger(line.split(":")[1]);
line = buf.readLine();
n = new BigInteger(line.split(":")[1]);
line = buf.readLine();
combineSharesConstant = new BigInteger(line.split(":")[1]);
line = buf.readLine();
shares = new BigInteger(line.split(":")[1]);
for(int i=0; i<5; i++){
line = buf.readLine();
viarray[i] = BigInteger.ZERO;
}
SecureRandom rnd = new SecureRandom();
PaillierPrivateThresholdKey result = new PaillierPrivateThresholdKey(n, l, combineSharesConstant, w, v,
viarray, shares, 2, rnd.nextLong());//il 2 qua รจ il nodeID
}catch(IOException e){
System.out.println(e);
}
break;
case 2: // Session Secret received
break;
case 3: //PDM received
break;
}//end switch
}catch(IOException ioe){
System.out.println(ioe);
}catch(ClassNotFoundException cnfe){
System.out.println(cnfe);
}finally {
try{
if (dis != null) dis.close();
if (dos != null) dos.close();
if (clientSocket!=null) clientSocket.close();
}catch (IOException e){
System.out.println(e);
}
}
}
}
I'd like to pass l, w, v, n and so on to let my main thread do some processing. How can i modify my code to do it?
Use a Callable instead of a Runnable.
Bundle the 4 variables into a class and implement Callable<YourNewClass>. Run your callable with an executor and you'll get the results in a Future<YourNewClass>.

Java Socket - send many message

I have two simple classes:
Client:
public static void main(String[] args) throws IOException {
InetAddress addr = InetAddress.getByName(null);
Socket socket = null;
try {
socket = new Socket(addr, 1050);
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
in = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter( socket.getOutputStream());
BufferedWriter bw = new BufferedWriter(osw);
out = new PrintWriter(bw, false);
stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
// read user input
while (true) {
userInput = stdIn.readLine();
System.out.println("Send: " + userInput);
out.println(userInput);
out.flush();
String line = in.readLine();
while(line != null){
System.out.println(line);
line = in.readLine();
}
System.out.println("END");
}
}
catch (UnknownHostException e) {
// ...
} catch (IOException e) {
// ...
}
// close
out.close();
stdIn.close();
socket.close();
}
Server:
OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter out = new PrintWriter(bw, /*autoflush*/true);
private void sendMessage(String msg1, String msg2) {
out.println(msg1);
// empy row
out.println("");
out.println(msg2);
}
The user enters a message, and this is sent to the server. Then, the server responds with N messages.
After the first request, the client stops and is never printed the word "END".
How do I send multiple messages at different times, with only one socket connection?
Firstly, you don't need to send an empty row, because you are sending by "line" and recieving by "line".
out.println(msg1);
out.println(msg2);
and
userInput = stdIn.readLine();
Here, userInput will only equal msg1
What I would recommend, would be not to loop on stdIn.readLine() = null, but have the client send, for example, "END_MSG", to notify the server that it will not send anymore messages.
Perhaps something like...
SERVER:
userInput =stdIn.readLine();
if(userInput.Equals("START_MSG");
boolean reading=true;
while(reading)
{
userInput=stdIn.readLine();
if(userInput.Equals("END_MSG")
{
//END LOOP!
reading = false;
}
else
{
//You have received a msg - do what you want here
}
}
EDIT:CLIENT:
private void sendMessage(String msg1, String msg2) {
out.println("START_MSG");
out.println(msg1);
out.println(msg2);
out.println("END_MSG");
}
(It also looks like in your question to have mixed up the client and the server?)

Java connection to Jabber with only Sockets

What type of stream can I use to send a request message over a tcp socket to jabber.
I'm writing a string with xml format.
I cant use any libraries. It has to be pure java sockets.
the following is the code which i used. But the response for the second xml request is null
try {
Socket s = new Socket("195.211.49.6", 5222);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.println("<stream:stream to='nimbuzz.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
out.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(s
.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
out.println("<iq type='set' xml:lang='en' id='terms' to='nimbuzz.com'><query xmlns='jabber:iq:auth'><username>username</username><password>password</password><resource>resource</resource></query></iq>");
out.flush();
reader = new BufferedReader(new InputStreamReader(s
.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
this is what i have implemented in c#, it works quite fast too.
Socket m_socWorker;
try
{
m_socWorker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
string ipString = "195.211.49.6";
string str2 = "5222";
int port = Convert.ToInt16(str2, 10);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ipString), port);
m_socWorker.Connect(remoteEP);
string page=string.Empty, page1=string.Empty, page2=string.Empty;
string s = "<stream:stream to='nimbuzz.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>";
byte[] bytes = Encoding.UTF8.GetBytes(s);
byte[] buffer = new byte[0x4b38];
m_socWorker.Send(bytes, bytes.Length, SocketFlags.None);
int count = 0;
count = m_socWorker.Receive(buffer, buffer.Length, SocketFlags.None);
page = page + Encoding.ASCII.GetString(buffer, 0, count);
byte[] buffer3 = new byte[0x4b38];
int num2 = 0;
num2 = m_socWorker.Receive(buffer3, buffer3.Length, SocketFlags.None);
page1 = page1 + Encoding.ASCII.GetString(buffer3, 0, num2);
if (page1.Replace("\"", "'").IndexOf("<stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism><mechanism>PLAIN TEXT</mechanism></mechanisms><register xmlns='http://jabber.org/features/iq-register'/></stream:features>", 0) != 0)
{
string str3 = "<iq type='set' xml:lang='en' id='Nimbuzz_Login' to='nimbuzz.com'><query xmlns='jabber:iq:auth'><username>username</username><password>password</password><resource>resource</resource></query></iq>";
byte[] buffer4 = new byte[0x30d40];
buffer4 = Encoding.UTF8.GetBytes(str3);
byte[] buffer5 = new byte[0x4b38];
m_socWorker.Send(buffer4, buffer4.Length, SocketFlags.None);
int num3 = 0;
num3 = m_socWorker.Receive(buffer5, buffer5.Length, SocketFlags.None);
page2 = Encoding.ASCII.GetString(buffer5, 0, num3);
string str4 = page2.Replace("\"", "'");
int num4 = 1;
}
}
catch (SocketException)
{
}
catch (Exception)
{
}
You are attaching a 2nd BufferedReader (InputStreamReader (...)) to your stream.
Probably the answer to your second request is being consumed and lost in the first buffer.
Try re-using your initial BufferedReader reader; to read the answer to the second message. Remember that XMPP is a single bi-directional stream, so all interaction happens through the same socket throughout the lifetime of your connection.
-- EDIT --
Q: How should the second request be like?
A: Editing your code to give you a starting point (not checked for compilation, just to give you the idea on how to proceed):
private static final int BUFFER_SIZE = 1024;
// Encapsulate the read process
private String readData(Reader reader) throws IOException {
StringBuilder result = new StringBuilder();
char[] buffer = new char[BUFFER_SIZE]; // [note1]
while (reader.ready()) { // [note2]
int charsRead = reader.read(buffer,0,BUFFER_SIZE-1));
if (charsRead > 0) {
result.append(buffer,0,charsRead);
}
}
return result.toString();
}
public void readStuff() {
try {
Socket s = new Socket("195.211.49.6", 5222);
PrintWriter out = new PrintWriter(s.getOutputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(s.getInputStream()));
out.println("<stream:stream to='nimbuzz.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
out.flush();
// Read out the data and print it to the console
System.out.println(readData(bufferedReader));
// Second request over the same socket
out.println("<iq type='set' xml:lang='en' id='terms' to='nimbuzz.com'><query xmlns='jabber:iq:auth'><username>username</username><password>password</password><resource>resource</resource></query></iq>");
out.flush();
// Read out the answer for the second result
System.out.println(readData(bufferedReader));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
Notes:
[1] This buffer can be reused across different requests. There's no actual need to recreate it every time this method is called. I left it there to provide you some anchoring with your C# code.
[2] You are checking for EOF in your code. This will potentially not happen in an XMPP connection. It's better to read the characters that are available in the stream until there're no more. Therefore I'm checking on reader.ready() instead of reader.read(...)>-1
See this question for further discussion on EOF: How do I recognize EOF in Java Sockets?

Categories