Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

problem with java.net.SocketException at client site

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
/*************************************************/
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic