aspose file tools*
The moose likes I/O and Streams and the fly likes Socket exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Socket exception" Watch "Socket exception" New topic
Author

Socket exception

bill Kal
Greenhorn

Joined: Aug 31, 2008
Posts: 1
Hello everybody!!!
here is my problem.
i have built a client apllication that copies a file(.txt) through a socket in a client ( it is a simulation program so both client and server run in my pc)

the problem that i have is this : in order for the copying to take place i create 2 objects in my configFile()

BufferedOutputStream byteOut = new BufferedOutputStream(clientSocket.getOutputStream());*
FileInputStream fin= new FileInputStream("C:\\askisi\\Client1HD\\config.txt");

to help me transfer the bytes from the client to the server

i already have two other objects in & out to help me exchange strings with my server.

when i finish my copy i close the byteOut object but then my programm crashes and it says that an exception took place the because socket is closed.

actually it crashes when it reaches the line fromServer=in.readLine(); in my searchFile method.

if i try open the in object again it doesn fix it it gets the same exception

and if i dont close the byteout object the file wont be copied

please help


this is my code

import java.io.*;
import java.net.*;
import java.util.*;

public class Client1
{
private Socket clientSocket ;
private PrintWriter out ;
private BufferedReader in ;
private Scanner stdIn;
private InetAddress host;
private String fromServer;
private String toServer;
private String clientName="Billy";
private int port;

public void runClient() throws IOException
{

host = InetAddress.getLocalHost();

try
{
System.out.println("Attempting to connect...");
clientSocket = new Socket(host.getHostName(), 12345);
System.out.println("Connected");
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

System.out.println("Got I/O Streams");
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: Billy.");
System.exit(1);
}

out.println(clientName);

fromServer=in.readLine();


System.out.println(fromServer);

stdIn = new Scanner(System.in);
toServer=stdIn.nextLine(); // epilogi "m" or "n"


while((!toServer.equalsIgnoreCase("m")) && (!toServer.equalsIgnoreCase("c")) ) // apofigi lathos input
{
System.out.println("You must type m or c");

}
out.println(toServer); // ������� �� m � �� c


if( toServer.equalsIgnoreCase("m")) // an dialiksei o xristis manual
{

fromServer=in.readLine();

System.out.println(fromServer);

manualConfig();

}

else if ( toServer.equalsIgnoreCase("c")) // an dialekseo o xristis configuration file
{

fromServer=in.readLine();
System.out.println(fromServer);

configFile();


}

/********************************ok****************************************/

searchFile();

out.close();
in.close();
clientSocket.close();
}

private void manualConfig() throws IOException
{
for (int i=0;i<=1;i++)
{
fromServer=in.readLine();
System.out.println(fromServer);

}

Scanner input = new Scanner(System.in);

while ( input.hasNext() )
{

toServer=input.nextLine();
out.println(toServer);
}
toServer = "O.K";
out.println(toServer);

}

private void configFile() throws IOException
{
* BufferedOutputStream byteOut = new BufferedOutputStream(clientSocket.getOutputStream());*
FileInputStream fin= new FileInputStream("C:\\askisi\\Client1HD\\config.txt");

int c;

while ((c=fin.read())!=-1)
* byteOut.write(c); *

byteOut.close();
fin.close();

}


private void searchFile() throws IOException
{
fromServer=in.readLine();
System.out.println(fromServer);

Scanner info = new Scanner(System.in);
toServer=info.nextLine();
out.println(toServer);

fromServer=in.readLine();
if ((fromServer.equalsIgnoreCase("To exeis")) || (fromServer.equalsIgnoreCase("Den iparxei")))
{
System.out.println(fromServer);
System.exit(0);
}

try
{
port=Integer.parseInt(fromServer);
}
catch(NumberFormatException e)
{
System.err.println("Error converting");
}

System.out.printf("%d\n", port);

}

public static void main(String[] args) throws Exception
{

Client1 c = new Client1();
c.runClient();
}

}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Please UseCodeTags. And while you're using those, please also put the indentation in the code. I would have put your code inside code tags but it wouldn't have made any difference.

Moving this to I/O / Streams.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
it crashes when it reaches the line fromServer=in.readLine();

Do you get an error message? Please copy and paste it here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket exception
 
Similar Threads
not able to enter selection on servlet
making Thread of methods
Server sending data to Clients Randomly
Data from server is not sent to client
client / server (communication problem...)