简体   繁体   中英

Sending and recieving data to and from server sockets

I hava a server socket and a client socket.I have created a buffered input and output stream for both the sockets.I have established a connection between them .Now i have sent data from the client socket to the server socket via the BufferedOutputStream and have read it from the server socket and displayed it .When i tried to send the data back to the client socket it throws exception saying the socket is closed . Here is the code Server application code:

   import java.io.*;
import java.net.*;
public class MyServer 
{
    public static void main(String args[]) throws Exception
    {
        int c;
        ServerSocket myServer=new ServerSocket(4341);
        Socket cSocket;
        cSocket =myServer.accept();     
        System.out.println("connection made");
        InputStream in = cSocket.getInputStream();
        OutputStream out = cSocket.getOutputStream();
        BufferedInputStream bin = new BufferedInputStream(in);
        BufferedOutputStream bout = new BufferedOutputStream(out);
        while ((c=bin.read())!=-1)
        {   
            //c=in.read();
            System.out.println((char)c);
            bout.write(c);
            //System.out.print("junaid");
        }
        System.out.println("connection made");
        bout.flush();
        cSocket.shutdownOutput();
        cSocket.close();
        myServer.close();
    }
}

Client application:

import java.io.*;
import java.net.Socket;
public class IOStreams
{
    public static void main(String args[]) throws Exception
    {
        int c;
        Socket s = new Socket("localhost",4341);
        InputStream in=s.getInputStream();
        OutputStream out=s.getOutputStream();
        BufferedInputStream bin = new BufferedInputStream(s.getInputStream());
        BufferedOutputStream bout = new BufferedOutputStream(s.getOutputStream());
        String myStr="what is google";
        byte buf[] = myStr.getBytes();
        try
        {
            bout.write(buf);
        }
        catch(Exception e)
        {
            System.out.print(e);
        }
        bout.flush();
        //InputStream in = s.getInputStream();
        //OutputStream out =s.getOutputStream();        
        s.shutdownOutput();
        while((c=bin.read()) !=-1)
        {
            //System.out.print("junaid");
            System.out.print((char) c);
        }
        //System.out.println(a);
        s.close();
    }
}

The problem is that when i only read from the server sockets input stream it works fine but when i also try to read from the client socket's input stream it throws the exception .i know i have closed the bufferedOutputStream of the client socket but if i dont close that it gets stuck in the while loop of the server socket. My aim is to sent data from the client to the server then display it and send that data back to the client and display it again .I have successfully sent the data to the server from the client and displayed it but i am unable to send it back to the client .What is wrong with my approach.I dont need a code i need to how data is sent and recieved without closing the connection.

Don't close the OutputStream in the client ( bout.close(); ) (at least not before invoking Socket.shutdownOutput() , see below).

From the Javadoc of Socket.getOutputStream() :

Returns an output stream for this socket.

If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the output stream's write operations will throw an java.nio.channels.IllegalBlockingModeException.

Closing the returned OutputStream will close the associated socket.

If you want to indicate that you're done writing to the socket, call Socket.shutdownOutput() instead. It's Javadoc states:

Socket.shutdownOutput() :

Disables the output stream for this socket. For a TCP socket, any previously written data will be sent followed by TCP's normal connection termination sequence.

After invoking Socket.shutdownOutput(), which waits for the data to be flushed, you can close the OutputStream , the InputStream , or the Socket . (it doesn't matter which you close - all three will end up closing the Socket )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM