Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > doubt about socket programing

Reply
Thread Tools

doubt about socket programing

 
 
chandu
Guest
Posts: n/a
 
      02-28-2006
my client side programme

import java.net.*;
import java.io.*;

class vissock1
{
public static void main(String args[]) throws IOException
{
Socket s=new Socket("172.18.19.203",80);
InputStream in=s.getInputStream();
OutputStream out=s.getOutputStream();
DataInputStream d=new DataInputStream(System.in);
int j;
for(int i=0;i<3;i++)
{
try
{
int k=Integer.parseInt(d.readLine());
out.write(k);
}
catch(Exception e)
{
System.out.println("enter valid no");
}
while((j=in.read())!=-1)
{
System.out.print((char) j);
}

}

}
}

my server side programme

import java.net.*;
import java.io.*;

class ser
{
public static void main(String args[]) throws IOException
{
ServerSocket s1=new ServerSocket(80);
Socket s=s1.accept();
InputStream in1=s.getInputStream();
OutputStream out1=s.getOutputStream();
while(1==1)
{
int j=in1.read();
if(j%2==0)
{
out1.write("even\n".getBytes());
}
else
{
out1.write("odd\n".getBytes());
}
}
}
}


i am able to get only one input from keyboard and accordingly the
response from server for that input.and then program is hanging
up...when i remove the code for reading server response from the for
loop(in client code) and keep out side the for loop i am getting
output.what is the reason.please help me.

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      02-28-2006

"chandu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> my client side programme
>
> import java.net.*;
> import java.io.*;
>
> class vissock1
> {
> public static void main(String args[]) throws IOException
> {
> Socket s=new Socket("172.18.19.203",80);
> InputStream in=s.getInputStream();
> OutputStream out=s.getOutputStream();
> DataInputStream d=new DataInputStream(System.in);
> int j;
> for(int i=0;i<3;i++)
> {
> try
> {
> int k=Integer.parseInt(d.readLine());
> out.write(k);
> }
> catch(Exception e)
> {
> System.out.println("enter valid no");
> }
> while((j=in.read())!=-1)
> {
> System.out.print((char) j);
> }


<snip>

> i am able to get only one input from keyboard and accordingly the
> response from server for that input.and then program is hanging
> up...when i remove the code for reading server response from the for
> loop(in client code) and keep out side the for loop i am getting
> output.what is the reason.please help me.


Your client loop to read the reply from the server is waiting for EOF. I
think you want to stop reading after \n instead. Hint: socket streams are
not packet-oriented at all. Also, DataInputStream.readLine() is
deprecated--look at switching to BufferedReader. Note also that if the user
enters an invalid number you program will still attempt to read a response
that will never come. Read reply should only be coupled with a successful
write--and you can use BufferedReader on the reply here also.

Cheers,
Matt Humphrey http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
 
chandu
Guest
Posts: n/a
 
      03-02-2006
thank you for your reply..can you suggest me any link for better
understainding of streams in java..

 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      03-02-2006

"chandu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> thank you for your reply..can you suggest me any link for better
> understainding of streams in java..
>


Try Roedy's pages on I/O. http://mindprod.com/jgloss/io.html They're not
stream-specific but it's a good starting place. And don't forget the Java
API. It may also seem that you should read some about TCP/IP because some
issues affect how you read data. http://mindprod.com/jgloss/tcpip.html

Cheers,
Matt Humphrey (E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
help in socket programing Clement C Programming 1 11-02-2007 11:16 PM
HEEELLLPPP socket programing instant messaging Sean C++ 6 01-16-2007 07:28 PM
Using SOcket programing to create multiple clients conatcting a single server zigbeedeep@gmail.com Java 2 11-12-2006 04:08 PM
Port Delegation in Socket Programing pmm C Programming 1 10-05-2006 07:43 AM



Advertisments