Client/Server app
Posted
by Knowing me knowing you
on Stack Overflow
See other posts from Stack Overflow
or by Knowing me knowing you
Published on 2010-06-09T07:39:22Z
Indexed on
2010/06/09
7:52 UTC
Read the original article
Hit count: 174
Guys could anyone tell me what's wrong am I doing here? Below are four files Client, main, Server, main. I'm getting an error on Client side after trying to fromServer.readLine().
Error: Error in Client Software caused connection abort: recv failed
package client;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client
{
private PrintWriter toServer;
private BufferedReader fromServer;
private Socket socket;
public Client( )throws IOException
{
socket = new Socket("127.0.0.1",3000);
}
public void openStreams() throws IOException
{
//
// InputStream is = socket.getInputStream();
// OutputStream os = socket.getOutputStream();
// fromServer = new BufferedReader(new InputStreamReader(is));
// toServer = new PrintWriter(os, true);
toServer = new PrintWriter(socket.getOutputStream(),true);
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void closeStreams() throws IOException
{
fromServer.close();
toServer.close();
socket.close();
}
public void run()throws IOException
{
openStreams();
String msg = "";
Scanner scanner = new Scanner(System.in);
toServer.println("Hello from Client.");
// msg = scanner.nextLine();
while (msg != "exit")
{
System.out.println(">");
// msg = scanner.nextLine();
toServer.println("msg");
String tmp = fromServer.readLine();
System.out.println("Server said: " + tmp);
}
closeStreams();
}
}
package server;
import java.net.*;
import java.io.*;
public class Server
{
private ServerSocket serverSocket;
private Socket socket;
private PrintWriter toClient;
private BufferedReader fromClient;
public void run() throws IOException
{
System.out.println("Server is waiting for connections...");
while (true)
{
openStreams();
processClient();
closeStreams();
}
}
public void openStreams() throws IOException
{
serverSocket = new ServerSocket(3000);
socket = serverSocket.accept();
toClient = new PrintWriter(socket.getOutputStream(),true);
fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void closeStreams() throws IOException
{
fromClient.close();
toClient.close();
socket.close();
serverSocket.close();
}
public void processClient()throws IOException
{
System.out.println("Connection established.");
String msg = fromClient.readLine();
toClient.println("Client said " + msg);
}
}
package client;
import java.io.IOException;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{
Client client = new Client();
client.run();
}
catch(IOException e)
{
System.err.println("Error in Client " + e.getMessage());
}
}
}
package server;
import java.io.IOException;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Server server = new Server();
try
{
server.run();
}
catch(IOException e)
{
System.err.println("Error in Server " + e.getMessage());
}
}
}
© Stack Overflow or respective owner