problem with java.net.SocketException at client site
khurram mirza
Greenhorn
Joined: Jun 10, 2001
Posts: 25
posted
0
i 've to read file content from client site and write it to server site file.but at client site code throws SocketException saying connection reset by peer:socket write error. here is the client site code: try{ Socket socket=new Socket("192.168.0.106",1024); FileInputStream filein=new FileInputStream("c://source.doc"); OutputStreamWriter out=new OutputStreamWriter(socket.getOutputStream()); int ch; while((ch=filein.read())>=0) out.write(ch); out.close(); filein.close(); socket.close(); } catch(SocketException socket){System.out.println("SocketException caught at client:"+socket);} catch(IOException io){System.out.println(io);} here is server site code try{ ServerSocket ssocket=new ServerSocket(1024,1); Socket socket=ssocket.accept(); FileWriter fileout=new FileWriter("c://destination.doc"); BufferedReader data=new BufferedReader(new InputStreamReader(socket.getInputStream())); String text=data.readLine(); fileout.write(text); fileout.close(); } catch(SocketException socket){System.out.println("SocketException caught at server:"+socket);} catch(IOException io){System.out.println("IOException caught:"+io);} thanks in advance;
Ravi Verma
Ranch Hand
Joined: Aug 30, 2001
Posts: 42
posted
0
Try this way it should work. bye rk_gadapa@yahoo.com /*********************************/ //package net.socket; import java.io.*; import java.net.*; public class Client { private String sIPAddress = "localhost"; private int iPort = 9999;
public Client(String filepath) { } public void SendRequest() { try { FileInputStream fis = new FileInputStream ("Trace.txt"); // put in the local directory of this source byte[] i = new byte[fis.available()]; fis.read(i); fis.close(); request = i; ClntSocket = new Socket(sIPAddress,iPort);
public void StartServer() { try { ServerSoc = new ServerSocket(iPort); System.out.println("Server Started at PORT : "+iPort); crtThread = new Thread(this); crtThread.start(); } catch(IOException e) { System.out.println("Couldn't create Input Stream for the Client "+e.getMessage()); }
while((ClientSoc != null )&&(is != null)&&(os != null)) { int len = is.readInt(); request = new byte[len]; is.read(request,0,len); String s = new String (request); System.out.println("In value...."+s); os.writeBoolean(true); is.close(); os.flush(); os.close(); ClientSoc.close(); ClientSoc = null; } } catch(IOException e) { System.out.println("Couldn't create Input Stream for the Client "+e.getMessage()); } } }
public static void main(String args[]) { Server server = new Server(9999); server.StartServer(); } } /*************************************************/