Writing xml message & reading response from server using sockets

Joined
Nov 14, 2016
Messages
1
Reaction score
0
Hi, may anyone please assist. I am coding a client application to send an xml message to a server
with the code below. I do not get response from the server. I always get output that no response from server. xml message is :
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<request>
<EventType>Authentication</EventType>
<event>
<UserPin>12345</UserPin>
<DeviceId>12345<DeviceId>
<DeviceSer>ABCDE</DeviceSer>
<DeviceVer>ABCDE</DeviceVer>
<TransType>Users</TransType>
</Event>
</request>


//this is my code

public class SocketConnect {
public static void main(String[] args) throws IOException{
try{
Socket clientSocket = new Socket(serverName,
,port); //connect socket to the server
System.out.println("All good");
System.out.println("Socket is connected to " + clientSocket.getInetAddress() +
" port is "+ clientSocket.getPort());
StringBuilder xmlRequest = new StringBuilder();// to read request from the file
String requestLine;//read lines in xml message frm XmlMsg.txt
String ServResponse;// to write response from server to file README.txt
File requestFile = new File("XmlMsg.txt");//create File object to read XML request
//attach File Object to the inputstream Object to read from file
InputStream istream = new FileInputStream(requestFile);
//create object to read line by line in the XmlMsg.txt file
BufferedReader requestToServer = new BufferedReader(new InputStreamReader(istream));
//create object to write server response line by line into the README.txt file
PrintWriter responseFrmServer = new PrintWriter("README.txt");
//create object to send request to server
PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream());
//create object to read response from server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
try{
//reading request from file XmlMsg.txt & sending it to server line by line
while((requestLine = requestToServer.readLine()) != null){
xmlRequest.append(requestLine);//build the xml message
responseFrmServer.println(xmlRequest);

}
outToServer.println(xmlRequest.toString());//send xml request to server
outToServer.flush();
clientSocket.shutdownOutput();
if((ServResponse = inFromServer.readLine()) == null){
System.out.println("no response from server");
}
System.out.println("Server Response is written in README.txt.");
}catch(IOException e){
System.err.println("Error:" + e.getMessage());
}finally{
try{
istream.close();
responseFrmServer.close();
outToServer.close();
inFromServer.close();
}catch(IOException e){
System.out.println("File did not close");
System.exit(1);
}
}
}catch(UnknownHostException e){
System.out.println(" Ip address not known");
System.exit(1);
}catch(IOException e){
System.err.println("Error");
e.printStackTrace();
System.exit(1);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top