Article View: borland.public.jbuilder.java.language
Article #18800Socket problems...
From: Alessandro Fustini
Date: Thu, 16 Apr 1998 00:00
Date: Thu, 16 Apr 1998 00:00
171 lines
4880 bytes
4880 bytes
Hy,
I have some problems with Read and Write Object with a Socket.
I have to read a list of file in the server disk and transfert it
to a client.I use a Hashtable and serialaize it.
If I read or write only the server works very fine but
if i read object and after write object an exception occurs :
java.lang.NullPointerException
at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
at java.net.SocketOutputStream.<init>(SocketOutputStream.java:49)
at
java.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:401)
at java.net.Socket.getOutputStream(Socket.java:302)
at
it.proxys.MultiFileThreadCopier.run(MultiFileThreadCopier.java:34)
in the code :
out = new ObjectOutputStream(socket.getOutputStream()); << this is
the line 34 where exception occurs
Thanks a lot for all help.
Alex
fustini@tin.it
import java.io.*;
import java.net.*;
import java.util.*;
public class MultiFileCopier {
final static String ProcName = "MultiFileCopier";
public static void main(String[] args) {
Debug Debug1 = null;
int listPort = 4444;
int counter = 0;
try
{
ServerSocket serverSocket = null;
boolean listening = true;
try
{
serverSocket = new ServerSocket(listPort);
} catch (IOException ex1) {
System.exit(1);
} catch (Exception ex2) {
System.exit(1);
}
while (listening)
{
Socket clientSocket = null;
try
{
clientSocket = serverSocket.accept();
} catch(IOException ex1)
{
continue;
}
counter++;
System.out.print("Contatore : ");
System.out.print(counter);
// creazione oggetto che gestisce le transazioni
new MultiFileThreadCopier(clientSocket,counter).start();
if (counter > 31998)
counter = 0;
}
try {
serverSocket.close();
} catch(IOException ex1)
{
}
} catch (Exception ex)
{
} finally
{
dos.close();
}
}
}
class MultiFileThreadCopier extends Thread {
Socket socket ;
Debug Debug1 = null;
MultiFileThreadCopier(Socket socket,int counter)
{
super("FileCopy"+ new Integer(counter).toString());
this.socket = socket;
}
public void run()
{
ObjectInputStream ois = null;
ObjectOutputStream out = null;
try
{
Debug1 = new Debug(System.err);
Debug1.setShowMessageOutPut(true);
Hashtable Hashret = new Hashtable();
ois = new ObjectInputStream(socket.getInputStream());
Hashtable t = (Hashtable)ois.readObject();
ois.close();
Hashret.put("return","true");
System.err.println(Hashret);
out = new ObjectOutputStream(socket.getOutputStream()); << this is
the line 34 where exception occurs
out.writeObject(Hashret);
out.close();
} catch (Exception ex2)
{
ex2.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
public class MultiFileClient {
public static void main(String[] args) {
Socket kksocket = null;
ObjectInputStream ois = null;
ObjectOutputStream ous = null;
try {
kksocket = new Socket("localhost",4444);
Hashtable t = new Hashtable();
t.put("sessionid","");
t.put("command","GET DIR FILES");
ous = new ObjectOutputStream(kksocket.getOutputStream());
ous.writeObject(t);
ous.close();
t.clear();
ois = new ObjectInputStream(kksocket.getInputStream());
t = (Hashtable)ois.readObject();
ois.close();
Enumeration enum2 = t.elements();
for (Enumeration enum = t.keys() ; enum.hasMoreElements() ;) {
String FileName = (String) enum.nextElement();
String SFileSizeAndCRC = (String) enum2.nextElement();
System.out.println(FileName+ " "+ SFileSizeAndCRC);
}
} catch (UnknownHostException ex1)
{
System.err.println(ex1.toString());
} catch (IOException ex2)
{
System.err.println(ex2.toString());
} catch (Exception ex3)
{
System.err.println(ex3.toString());
}
}
}
Technical Details
Message-ID:
<35361A21.C500C6D1@tin.it>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,f8d273ae612f1fa5,start X-Google-Attributes: gidf74b3,public From: Alessandro Fustini <fustini@tin.it> Subject: Socket problems... Date: 1998/04/16 Message-ID: <35361A21.C500C6D1@tin.it> X-Deja-AN: 344678923 Content-Transfer-Encoding: 7bit Mime-Version: 1.0 Reply-To: fustini@tin.it Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language