my dog learned polymorphism*
The moose likes Sockets and Internet Protocols and the fly likes problem with java.net.SocketException at client site Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "problem with java.net.SocketException at client site" Watch "problem with java.net.SocketException at client site" New topic
Author

problem with java.net.SocketException at client site

khurram mirza
Greenhorn

Joined: Jun 10, 2001
Posts: 25
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
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;

private boolean response;
private byte request[] =null;
String filepath = null;

Socket ClntSocket = null;
DataInputStream is = null;
DataOutputStream os = null;

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);

os = new DataOutputStream (ClntSocket.getOutputStream());
is = new DataInputStream(ClntSocket.getInputStream());

if ((ClntSocket != null) && (os != null))
{
os.writeInt(request.length);
os.write(request,0,request.length);
}

RecieveResponse();
System.out.println("The response is : "+response);

os.flush();
os.close();
is.close();

}
catch(UnknownHostException e)
{
System.out.println("Can't find the Host : "+ClntSocket.getInetAddress().toString()+"****"+e.getMessage());
}
catch(IOException e)
{
System.out.println("Couldn't create Input Stream for the host : "+ClntSocket.getInetAddress().toString()+"*******"+e.getMessage());
}
}
public void RecieveResponse()
{
try
{
response = is.readBoolean();
}
catch(IOException e)
{
System.out.println("Couldn't create Input Stream for the host : "+ClntSocket.getInetAddress().toString()+"*******"+e.getMessage());
}
}

public static void main(String args[])
{
Client client = new Client();
client.SendRequest();
}
}
/******************************************************/
//package net.socket;
import java.io.*;
import java.net.*;
//import security.*;
public class Server implements Runnable
{
private int iPort = 9999;
private byte request[] = null;

ServerSocket ServerSoc = null;
Socket ClientSoc = null;
DataInputStream is ;
DataOutputStream os;

Thread crtThread = null;
boolean contn = true;

public Server()
{
//Default Constructor
}

public Server(int iPort)
{
this.iPort = 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());
}

}
public void run()
{
while(contn)
{
try
{
ClientSoc = ServerSoc.accept();

is = new DataInputStream (ClientSoc.getInputStream());
os = new DataOutputStream (ClientSoc.getOutputStream());

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();
}
}
/*************************************************/


Ravi
 
Don't get me started about those stupid light bulbs.
 
subject: problem with java.net.SocketException at client site