I have Web Application server, which takes parameters and pass them to Client class. When calling the exactly same Client class in normal Java Application, everything works fine, but when calling the Client from servlet, the Client-Server conversation always hangs for either of two reasons:
Certain message (always the same one) sent from server is oddly formated and Client cannot process it.
If the message aforesaid is correctly formated, when the Client responds to it, the server never replies.
Here is the part of Client responsible for the connection:
int key;
int messCount=0;
int count=0;
ConnCrypto m = new ConnCrypto();
String[] s = {};
GameCrypto g = new GameCrypto(s);
int i = g.init();
Socket tcp;
BufferedReader reader;
BufferedWriter writer;
CryptHandler crypt= new CryptHandler();
static int err=0;
void setup() throws IOException {
tcp = new Socket(serverIp, serverPort);
InputStream InputStream = tcp.getInputStream();
OutputStream OutputStream = tcp.getOutputStream();
InputStreamReader InputStreamReader = new InputStreamReader(InputStream);
OutputStreamWriter OutputStreamWriter = new OutputStreamWriter(OutputStream);
reader = new BufferedReader(InputStreamReader);
writer = new BufferedWriter(OutputStreamWriter);
}
void send(String z) throws IOException {
System.out.println("Send:\t" + z);
z = m.encrypt(z);
writer.write(z);
writer.newLine();
writer.flush();
}
void crypt(String z) throws IOException {
System.out.println("Send:\t" + z);
z = crypt.crypt(z);
z = "d " + messCount + " "+(char)1 + z;
send(z);
messCount++;
}
void crypt2(String z) throws IOException {
z = g.encrypt(z);
z = "d " + messCount + " " + z;
send(z);
messCount++;
}
String recv() throws IOException {
char chr;
StringBuilder sb= new StringBuilder();
while((chr=(char)reader.read())!=0x0A)
sb.append(chr);
String str=sb.toString();
if (key != 0) {
str = m.decrypt(str);
int gg = str.charAt(0);
if (gg == 100) {
System.out.print("Raw:\t"+str);
str = "d " + crypt.decrypt(str.substring(5));
}
}
else if (str.charAt(0) == 99) {
key = Integer.parseInt(str.substring(5));
m.init(i, key);
}
System.out.println("Recv:\t" + str);
return str;
}
So is is it bad to use the Socket object, or is there something else in my code I should avoid?
Related
I am writing tcp client-server program. When the client types "Hello" the server returns a list of files and directories of his current directory. When the client types "FileDownload " it downloads the selected file from the server.
When I type "Hello" it works fine, but when I type "FileDownload ", on the server side it runs twice the else if(received.contains("FileDownload")) block. Because of this the server is sending twice the data which is causing other issues on the client side.
Here is the server code:
public static void main(String[] args) throws IOException {
ServerSocket servSock = new ServerSocket(1333);
String received="";
String[] s = null;
File[] f1;
int i=0;
File f=new File(System.getProperty("user.dir"));
f1=f.listFiles();
for(File f2:f1) {
if(f2.isDirectory())
System.out.println(f2.getName() + "\t<DIR>\t" + i);
if(f2.isFile())
System.out.println(f2.getName() + "\t<FILE>\t" + i);
i++;
}
while (true) {
Socket client = servSock.accept();
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
PrintWriter pw = new PrintWriter(out, true);
s=f.list();
while(true) {
received = reader.readLine();
if(received.equals("END")) break;
if(received.equals("Hello")) {
System.out.println("Hello-Start");
int length=f1.length;
pw.println(length);
i=0;
for(File f2:f1) {
if(f2.isDirectory())
pw.println(f2.getName() + "\t<DIR>\t" + i);
if(f2.isFile())
pw.println(f2.getName() + "\t<FILE>\t" + i);
i++;
}
pw.println("Options: " + "\tFileDownload <FID>" + "\tFileUpload <name>" + "\tChangeFolder <name>");
System.out.println("Hello-End");
}
else if(received.contains("FileDownload")) {
System.out.println("FileDownload-Start");
int j=-1;
try {
j=Integer.parseInt(received.substring(13).trim());
}catch(NumberFormatException e) {
System.err.println("error: " + e);
}
if(j>0 && j<s.length) {
FileInputStream fi=new FileInputStream(s[j]);
byte[] b=new byte[1024];
System.out.println("file: "+s[j]);
pw.println(s[j]);
fi.read(b,0,b.length);
out.write(b,0,b.length);
System.out.println("FileDownload-End");
}
}
Here is the client code:
public static void main(String[] args) throws IOException {
if ((args.length != 2))
throw new IllegalArgumentException("Parameter(s): <Server> <Port>");
Socket socket = new Socket(args[0], Integer.parseInt(args[1]));
Scanner sc = new Scanner(System.in);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
PrintWriter pw = new PrintWriter(out, true);
while(true) {
String msg="", received="";
String length;
msg = sc.nextLine();
pw.println(msg);
if(msg.equals("END")) break;
if(poraka.equals("Hello")) {
System.out.println();
length = reader.readLine();
for(int i=0;i<Integer.parseInt(length);i++) {
received = reader.readLine();
System.out.println(received);
}
System.out.println("\n"+reader.readLine());
}
else if(msg.contains("FileDownload")) {
System.out.println("FileDownload-Start");
pw.println(msg);
byte[] b=new byte[1024];
File file=new File(reader.readLine().trim());
System.out.println(file.getName().trim());
FileOutputStream fo=new FileOutputStream("D:\\Eclipse WorkSpace\\proekt\\src\\client\\"+file.getName().trim());
System.out.println("file: "+file.getName().trim());
in.read(b,0,b.length);
fo.write(b,0,b.length);
System.out.println("FileDownload-End");
}
I could not find what's causing this issue, so any help possible would be very highly appreciated!
It is because your client requests the data twice:
msg = sc.nextLine();
pw.println(msg); // <-- this is the first time
and then later
else if(msg.contains("FileDownload")) {
System.out.println("FileDownload-Start");
pw.println(msg); // <-- this is the second time
I am writing a server for a game which I am coding in java. The output stream won't actually send the information back to the client for some reason. I have tried everything, however closing the socket ends up in the program erroring because of the socket closing before it has written to the output stream. I am unable to figure out why.
EDIT: I have put a lot of the code in this gist. Also, for clarification, the response wasn't sending at all, even if I didn't close the socket. The client was simply waiting for an answer, and not receiving one.
Here is my code.
public class ServerThread extends Thread {
private Socket clientSocket;
private List<Player> players;
public Player player = null;
public ServerThread (Socket clientSocket, List<Player> players)
{
this.clientSocket = clientSocket;
this.players = players;
}
public void run()
{
try {
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream os = new DataOutputStream(clientSocket.getOutputStream());
String req = br.readLine();
br.close();
String response = buildResponse(req);
os.writeBytes(response);
os.flush();
System.out.println("Sending [ " + response + " ] to " + clientSocket.getInetAddress().getHostAddress());
player = addPlayerFromRequest(req);
} catch (IOException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
try {
clientSocket.close();
} catch (IOException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String buildResponse(String req)
{
List<Player> plays;
plays = players;
String name = req.split(",")[0];
String response = "";
if (plays.size() <= 1) {
return response;
}
for (int i = 0; i < plays.size(); i++) {
Player p = plays.get(i);
if (!p.name.equals(name)) {
response += p.name + "," + p.x + "," + p.y + "," + p.z + "," + p.rx + "," + p.rx + "," + p.rz + ";";
}
}
return response;
}
public Player addPlayerFromRequest (String req)
{
String[] list = req.split(",");
String user = list[0];
float x = Float.parseFloat(list[1]);
float y = Float.parseFloat(list[2]);
float z = Float.parseFloat(list[3]);
float rx = Float.parseFloat(list[4]);
float ry = Float.parseFloat(list[5]);
float rz = Float.parseFloat(list[6]);
return new Player(x, y, z, rx, ry, rz, user);
}
}
This code will throw SocketException: Socket closed because of br.close(), but assuming you've removed that, I suggest that your client is reading lines but you aren't sending lines. Add a line terminator to the message, or use BufferedWriter.newLine().
Hey Guys my following code is a proxy written in java.
Everytime I try to run it, it throws an String Index out of range:-1 exception, which I don't know how to handle.
I also nee to redirect the request to a specific webpage, if a "bad" word has been written in the URL or the content of the web page.
How do I do that?
Please Help me!
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ProxyServer{
//Create the Port the user wants the proxy to be on
static Scanner sc = new Scanner(System.in);
public static final int portNumber = sc.nextInt();
public static void main(String[] args) {
ProxyServer proxyServer = new ProxyServer();
proxyServer.start();
}
public void start() {
System.out.println("Starting the SimpleProxyServer ...");
try {
//bad list of words
String bad[]= new String[4];
bad[0]= "SpongeBob";
bad[1]= "Britney Spears";
bad[2]= "Norrköping";
bad[3]= "Paris Hilton";
ServerSocket serverSocket = new ServerSocket(ProxyServer.portNumber);// this is the socket of the proxy
System.out.println(serverSocket);
byte[] buffer= new byte [10000] ;
//
while (true) {
Socket clientSocket = serverSocket.accept(); // the client willl be the socket the proxy "accepts"
boolean badContent =false; //flag, if the input contains one of the bad words
InputStream inputstream = clientSocket.getInputStream(); // retreiving request from the client
int n = inputstream.read(buffer); //reading buffer and storing its size
String browserRequest= new String(buffer,0,n+1); //new String(buffer,0,n);
String realbrowserRequest =( browserRequest+"Connection close()");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream));
for (int j=0; j<bad.length;j++){ //checking of the URL contains the bad words
if(realbrowserRequest.contains(bad[j])){
badContent =true; // if yes the flag will be set to true
}
}
//if(badContent == true){
//System.out.println("bad detected");
// try
// {
// URL url = new URL( "http://www.ida.liu.se/~TDTS04/labs/2011/ass2/error2.html" );
//
// BufferedReader in = new BufferedReader(
// new InputStreamReader( url.openStream() ) );
//
// String s;
//
// while ( ( s = in.readLine() ) != null )
// System.out.println( s );
//
// in.close();
// }
// catch ( MalformedURLException e ) {
// System.out.println( "MalformedURLException: " + e );
// }
// catch ( IOException e ) {
// System.out.println( "IOException: " + e );
// }
// else{
System.out.println("Das ist der Browserrequest: \n"+realbrowserRequest);
System.out.println("Das ist der Erste Abschnitt");
int start = browserRequest.indexOf(("Host: ") + 6);
int end = browserRequest.indexOf('\n', start);
String host = browserRequest.substring(start, end-1 ); //retreiving host
if(badContent =true) //if the URL already contains inappropriate material
{
URL url = new URL( "http://www.ida.liu.se/~TDTS04/labs/2011/ass2/error2.html" );
host = url.getHost(); // set the host to the given one
}
System.out.println("Connecting to host " + host);
Socket hostSocket = new Socket(host, 80); //I can change the host over here
OutputStream HostOutputStream = hostSocket.getOutputStream();
// PrintWriter writer= new PrintWriter (HostOutputStream);
// writer.println();
System.out.println("Forwarding request to server");
HostOutputStream.write(buffer, 0, n);// but then the buffer that is fetched from the client remains same
HostOutputStream.flush();
InputStream HostInputstream = hostSocket.getInputStream();
OutputStream ClientGetOutput = clientSocket.getOutputStream();
System.out.println("Forwarding request from server");
do {
n = HostInputstream.read(buffer);
String vomHost = new String(buffer,0,n);
System.out.println("\nVom Host\n\n"+vomHost);
for(int i=0;i<bad.length;i++){
if(vomHost.contains(bad[i])){
badContent=true;
}
}
System.out.println("Receiving " + n + " bytes");
if (n > 0) { // && badContent == false
ClientGetOutput.write(buffer, 0, n);
}
} while (n>0 && badContent == false); //n>0&& badContent == false
if (badContent == true){
}
ClientGetOutput.flush();
hostSocket.close();
clientSocket.close();
System.out.println("End of communication");
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
The issue is here :
int start = browserRequest.indexOf(("Host: ") + 6);
This means you are doing this :
int start = browserRequest.indexOf("Host: 6");
6 is being concatenated to "Host : "
Try this :
int start = browserRequest.indexOf("Host: ") + 6;
There are many issues in your code:
1) Change the line :
int start = browserRequest.indexOf(("Host: ") + 6);
To:
int start = browserRequest.indexOf("Host:") + 6;
As mentioned in ToYonos's answer, becuase it is wrong.
2) You have to make sure that the two variables start and end are not equal to -1(if they doesn't exist in browserRequest):
if(end>start && start>0)
{
String host = browserRequest.substring(start, end-1 );
}
This will avoid many Exceptions.
The following code is a PHP one I need to convert to Java to write it to a socket connection
$info = chr(6).chr(0).chr(255).chr(255).'info';
On PHP it works perfectly with this code
$Socket = fsockopen($this->Host, $this->Port, $errno, $errstr, 5);
fwrite($Socket, chr(6).chr(0).chr(255).chr(255).'info');
I tried doing this
public static void main(String[] args) {
char a = 6;
char b = 0;
char c = 255;
try {
Socket test = new Socket("shadowcores.twifysoft.net", 7171);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(test.getOutputStream()));
write.write(Character.toString(a) + "." + Character.toString(b) + "." + Character.toString(c) + "." + Character.toString(c) + "." + "info");
BufferedReader read = new BufferedReader(new InputStreamReader(test.getInputStream()));
System.out.println(read.read());
test.close();
} catch (IOException e) {
e.printStackTrace();
}
}
With no success because I should be getting some information from the server after writing that message to it
How can I convert that PHP string to Java one?
Below is the code for a client and server which handles multi user chat. But when one client writes "quit" my others current connected client also terminates and I can't then connect another client. Can anybody help with this?
Here is my client code:
class TCPClientsc {
public static void main(String argv[]) throws Exception {
String modifiedSentence;
InetAddress inetAddress = InetAddress.getLocalHost();
System.out.println(inetAddress);
Socket clientSocket = new Socket(inetAddress, 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
CThread write = new CThread(inFromServer, outToServer, 0, clientSocket);
CThread read = new CThread(inFromServer, outToServer, 1, clientSocket);
}
}
class CThread extends Thread {
BufferedReader inFromServer;
DataOutputStream outToServer;
Socket clientSocket = null;
int RW_Flag;
public CThread(BufferedReader in, DataOutputStream out, int rwFlag, Socket clSocket) {
inFromServer = in;
outToServer = out;
RW_Flag = rwFlag;
clientSocket = clSocket;
start();
}
public void run() {
String sentence;
try {
while (true) {
if (RW_Flag == 0) {// write
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
sentence = inFromUser.readLine();
// System.out.println("Writing ");
outToServer.writeBytes(sentence + '\n');
if (sentence.equals("quit"))
break;
} else if (RW_Flag == 1) {
sentence = inFromServer.readLine();
if (sentence.endsWith("quit"))
break;
System.out.println("(received)" + sentence);
}
}
} catch (Exception e) {
} finally {
try {
inFromServer.close();
outToServer.close();
clientSocket.close();
} catch (IOException ex) {
Logger.getLogger(CThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Server code:
class TCPServersc {
static int i = 0;
static SThread tt[] = new SThread[100];
static SThread anot[] = new SThread[100];
public static void main(String argv[]) throws Exception {
String client;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true) {
Socket connectionSocket = welcomeSocket.accept();
i++;
System.out.println("connection :" + i);
BufferedReader inFromClient = new BufferedReader(newInputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
BufferedReader inFromMe = new BufferedReader(new InputStreamReader(System.in));
tt[i] = new SThread(inFromClient, outToClient, tt, 0, connectionSocket, i);
anot[i] = new SThread(inFromMe, outToClient, tt, 1, connectionSocket, i);
}
}
}
// ===========================================================
class SThread extends Thread {
BufferedReader inFromClient;
DataOutputStream outToClient;
String clientSentence;
SThread t[];
String client;
int status;
Socket connectionSocket;
int number;
public SThread(BufferedReader in, DataOutputStream out, SThread[] t, int status, Socket cn, int number) {
inFromClient = in;
outToClient = out;
this.t = t;
this.status = status;
connectionSocket = cn;
this.number = number;
start();
}
public void run() {
try {
if (status == 0) {
clientSentence = inFromClient.readLine();
StringTokenizer sentence = new StringTokenizer(clientSentence, " ");
// ///////////////////////////////////////////////////////////
if (sentence.nextToken().equals("login")) {
String user = sentence.nextToken();
String pass = sentence.nextToken();
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
int flag = 0;
while ((client = br.readLine()) != null) {
if ((user.equals(client.substring(0, 5))) && (pass.equals(client.substring(6, 10)))) {
flag = 1;
System.out.println(user + " has logged on");
for (int j = 1; j <= 20; j++) {
if (t[j] != null)
t[j].outToClient.writeBytes(user + " has logged on" + '\n');// '\n' is necessary
}
break;
}
}
if (flag == 1) {
while (true) {
clientSentence = inFromClient.readLine();
System.out.println(user + " : " + clientSentence);
for (int j = 1; j <= 20; j++) {
if (t[j] != null)
// '\n' is necessary
t[j].outToClient.writeBytes(user + " : " + clientSentence + '\n');
}
// if(clientSentence.equals("quit"))break;
}
}
}
}
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (status == 1) {
while (true) {
clientSentence = inFromClient.readLine();
if (clientSentence.equals("quit"))
break;
System.out.println("Server: " + clientSentence);
for (int j = 1; j <= 20; j++) {
if (t[j] != null)
t[j].outToClient.writeBytes("Server :" + clientSentence + '\n');// '\n' is necessary
}
}
}
} catch (Exception e) {
} finally {
try {
// System.out.println(this.t);
inFromClient.close();
outToClient.close();
connectionSocket.close();
} catch (IOException ex) {
Logger.getLogger(SThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
This code has a number of problems.
First off, in the future, please post smaller, concise code fragments that are well formatted. I just had to basically reformat everything in your post.
I see a couple of places where you are catching but doing nothing with exceptions. This is tremendously bad practice. At the least you should be printing/logging the exceptions you catch. I suspect this is contributing to your problems.
I find the RW_Flag very confusing. You should have two client threads then. One to write from System.in to the server and one to read. Don't have one client thread which does 2 things. Same with status flag in the server. That should be 2 different threads.
Instead of int flag = 0; in the server, that should be boolean loggedIn;. Make use of booleans in Java instead of C-style flags and use better variable names. The code readability will pay for itself. Same for status, RW_flag, etc..
Instead of huge code blocks, you should move contiguous code out to methods: handleSystemIn(), handleClient(), talkToServer(). Once you make more methods in the your code, and shrink down the individual code blocks, it makes it much more readable/debuggable/understandable.
You need to have a synchronized (tt) block around each usage of that array. Once you have multiple threads that are all using tt if the main accept thread adds to it, the updates need to be synchronized.
I don't immediately see the problem although the spagetti code is just too hard to parse. I suspect you are throwing and exception somewhere which is the reason why clients can't connect after the first one quits. Other than that, I would continue to use liberal use of System.out.println debugging to see what messages are being sent where.