Freeze on InputStream creation

Ok, i fixed the freezing problem, but another problem has arisen.

All i want to do is send an integer from the server... to the client. It's very simple. But all the methods from the StreamReaders... that are all like "read()" and "readLine()" Are they waiting for input? because i don't want to input. I want to send an integer IN THE CODE from the server to the client via a socket... It doesn't seem to work. Someone help me out please. Thanks

Which classes should i use to READ AND WRITE integers from clients - servers back and forth?

[575 byte] By [PaRlOaGna] at [2007-11-27 8:29:57]
# 1
DataInputStream and DataOutputStream.
ejpa at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...
# 2

Hi,

I am sending the sample server and client program to send and retrieve the integers.

ServerEx.java

-

import java.net.*;

import java.io.*;

public class ServerEx

{

ServerSocket serverSocket=null;

Socket socket=null;

InputStream is;

OutputStream os;

public ServerEx(int port) throws Exception

{

serverSocket=new ServerSocket(port);

}

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

{

int port=7777;

if(args.length>0)

port=Integer.parseInt(args[0]);

ServerEx s=new ServerEx(port);

s.getClient();

s.getStreams();

for(int i=1;i<=10;i++)

s.sendData(i);

for(int i=1;i<=10;i++)

System.out.println(s.getData());

}

public void getClient() throws Exception

{

System.out.println("Waiting for client");

socket=serverSocket.accept();

System.out.println("Client found");

}

public void getStreams() throws Exception

{

if(socket!=null){

is=socket.getInputStream();

os=socket.getOutputStream();

System.out.println("Got the streams");

}else

System.out.println("Client not available");

}

public void sendData(int num) throws Exception

{

os.write(num);

}

public int getData() throws Exception

{

return is.read();

}

}

ClientEx.java

-

import java.net.*;

import java.io.*;

public class ClientEx

{

Socket socket=null;

InputStream is;

OutputStream os;

public ClientEx(int port) throws Exception

{

socket=new Socket("localhost",port);

System.out.println("Connected to the server");

}

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

{

int port=7777;

if(args.length>0)

port=Integer.parseInt(args[0]);

ClientEx c=new ClientEx(port);

c.getStreams();

for(int i=1;i<=10;i++)

System.out.println(c.getData());

for(int i=1;i<=10;i++)

c.sendData(i);

}

public void getStreams() throws Exception

{

if(socket!=null){

is=socket.getInputStream();

os=socket.getOutputStream();

System.out.println("Got the streams");

}else

System.out.println("Client not available");

}

public void sendData(int num) throws Exception

{

os.write(num);

}

public int getData() throws Exception

{

return is.read();

}

}

I hope I helped you. If you have any queries please let me know.

Thanks & Regards,

Santhosh Reddy Mandadi

santhosh-mcaa at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...
# 3

Alrighty, i got the integer transmitting to work. But whenever i try to make an ObjectOutputStream it always get's an IOException. Do i need to have all other Streams closed? because i'm pretty sure i have none others open. I could just be cleaning these things up wrong or something. Can i get a variable declaration for an ObjectOutputStream, also, do i need to specify a portnum with it or just a socket.

PaRlOaGna at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...
# 4
You have to have an ObjectOutputStream at the sending end if you use an ObjectInputStream at the receiving end.You don't specify either a port number or a socket, you give it an InputStream or an OutputStream, which you can get from the socket.
ejpa at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...
# 5

What if i want to have two different ObjectOutputStreams and two different ObjectInputStreams. Because i want the client to be able to RECEIVE ArrayList objects, and to be able to SEND CustomPacket objects. But the server should be able to RECEIVE CustomPacket objects and SEND ArrayList objects. Any suggestions?

PaRlOaGna at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...
# 6
Why would you need two different streams for that? - apart from the fact that it won't work. Just send the objects via the same stream. At the receiver, use instanceof to find out what the received object was.
ejpa at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...
# 7

Thanks alot for the response... could you possibly do me one more favor? :P uhm, for some reason this chunk of code isn't working, it always stops on InputStream... tell me if i'm doing something wrong. :D

try {

System.out.println("connecting...");

s = new Socket(InetAddress.getByName("localhost"), 1800);

System.out.println("Connected!");

System.out.println("Creating DataInput stream...");

DataInputStream in = new DataInputStream(s.getInputStream());

System.out.println("InputStream Created!");

System.out.println("Reading int...");

index = in.readInt();

in.close();

System.out.println("Client index: " + index);

System.out.println("Creating ObjectOutputStream...");

oos = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));

oos.flush();

System.out.println("ObjectOutputStream created!");

System.out.println("Creating ObjectInputStream...");

ois = new ObjectInputStream(s.getInputStream());

System.out.println("ObjectInputStream created!");

} catch(IOException e) {System.err.println(e.toString()); System.exit(-1);}

this is what i get when i run it:

connecting...

Connected!

Creating DataInput stream...

InputStream Created!

Reading int...

Client index: 2

Creating ObjectOutputStream...

ObjectOutputStream created!

Creating ObjectInputStream...

java.net.SocketException: Software caused connection abort: recv failed

I've already googled the error couldn't find much on it...

Message was edited by:

PaRlOaGn

PaRlOaGna at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...
# 8
Ok, so apparently if i close the first DataStream which is what's written up there, it throws a Socket Closed, error, but if i don't close it, it throws a RecV error, and..... is that some sort of error saying there's more than one stream open on it at once? please help.
PaRlOaGna at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...
# 9
Don't close the DataInputStream. That closes the socket.You don't even need it. ObjectInputStream extends DataInputStream so you can use ObjectInputStream for everything. Similarly for DataOutputStream and ObjectOutputStream.
ejpa at 2007-7-12 20:20:24 > top of Java-index,Core,Core APIs...