Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 6th, 2006, 11:09 PM
Friend of Wrox
 
Join Date: Jan 2006
Location: Hardwar, Uttranchal, India.
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default Need help for Socket Program:

Hi all !

  I've a socket programe to send the data from Server to Client but I'm not able to send it to client. In my programe data has to go to client side when I type the "exit" but as I send the data I get the error in Client side which is:

Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream. java:168)
        at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Strea mDecoder.java:411)
        at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Stream Decoder.java:453)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:1 83)
        at java.io.InputStreamReader.read(InputStreamReader.j ava:167)
        at java.io.BufferedReader.fill(BufferedReader.java:13 6)
        at java.io.BufferedReader.readLine(BufferedReader.jav a:299)
        at java.io.BufferedReader.readLine(BufferedReader.jav a:362)
        at clnt.main(clnt.java:13)


My server side code is:

import java.net.*;
import java.io.*;
class svr
{
    public static void main(String str[]) throws Exception
    {
        BufferedReader BR;
        ServerSocket s_soc;
        Socket soc;
        s_soc = null;
        s_soc = new ServerSocket(90);
        soc = s_soc.accept();

        BR = new BufferedReader(new InputStreamReader(System.in));

        PrintWriter pw = new PrintWriter( soc.getOutputStream(), true);

        while( (BR.readLine()) != null)
        {
            if (BR.readLine().equals("exit"))
                break;
            else
                pw.write(BR.readLine());
                pw.flush();
        }
    }
}

and my client side code is:

import java.net.*;
import java.io.*;
class clnt
{
    public static void main(String str[]) throws Exception
    {
        Socket sock = new Socket(InetAddress.getLocalHost(), 90);
        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
        String getOutput;
        while( (getOutput = br.readLine()) != null )
        {
            out.write(getOutput);
        }
        out.close();
        br.close();
        sock.close();
    }
}



pls help me...
Thanks for any suggestion......

DPK..
__________________
DPK..
Reply With Quote
  #2 (permalink)  
Old May 17th, 2006, 01:41 AM
Registered User
 
Join Date: May 2006
Location: Chennai, Tamil Nadu, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
   This problem is occurring because of the problem in your code

you have been reading from the socket using BR.readLine in the while loop and then you are checking again by reading one more line in the if condition which means at this point it is reading the second line which might not be exit. it is giving this error because you are terminating the while loop using break without closing the socket connection and so the error..

your while loop should be something like this.

        String line = null;
        while( (line = BR.readLine()) != null)
        {
            if (line.equalsIgnoreCase("exit"))
                break;
            else
                pw.write(line);
                pw.flush();
        }

and you should wait for a few seconds before closing the connection so that termination is proper


the server code should look something like this:-
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
class Server extends Thread
{
    public void run()
    {
        try
        {
            Thread.sleep(2000);

        }
        catch(InterruptedException ie)
        {
            ie.printStackTrace();
        }
    }

    public static void main(String str[]) throws Exception
    {
        BufferedReader BR;
        ServerSocket s_soc;
        Socket soc;
        s_soc = null;
        s_soc = new ServerSocket(90);
        soc = s_soc.accept();

        BR = new BufferedReader(new InputStreamReader(System.in));

        PrintWriter pw = new PrintWriter( soc.getOutputStream(), true);

        String line = null;
        while( (line = BR.readLine()) != null)
        {
            if (line.equalsIgnoreCase("exit"))
            {
                (new Server()).start();
                soc.close();
                break;
            }
            else
                pw.write(line);
                pw.flush();
        }

        s_soc.close();
    }
}


Client Program:-
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
class Client
{
    public static void main(String str[]) throws Exception
    {
        Socket sock = new Socket(InetAddress.getLocalHost(), 90);
        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
        String getOutput;
        while( (getOutput = br.readLine()) != null )
        {
            System.out.println(getOutput);
            out.write(getOutput);
        }
        out.close();
        br.close();
        sock.close();
    }
}

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket Progrmming sudhirbharti C# 2005 2 February 21st, 2008 03:54 AM
Socket connection Lizane Java GUI 1 September 23rd, 2007 11:11 PM
how to convert socket program into swings praneel Java GUI 0 September 13th, 2005 11:42 PM
Setup Project: Program not added in Start>Program arif_1947 VS.NET 2002/2003 2 March 31st, 2005 06:40 AM
Start a program inside another program Silje Classic ASP Professional 1 November 16th, 2004 02:08 AM



All times are GMT -4. The time now is 08:54 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.