简体   繁体   English

在服务器套接字之间发送和接收数据

[英]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 . 我有一个服务器套接字和一个客户端套接字。我已经为这两个套接字创建了一个缓冲的输入和输出流。我已经建立了它们之间的连接。现在我已经通过BufferedOutputStream从客户端套接字向服务器套接字发送了数据,并且从服务器套接字读取并显示它。当我尝试将数据发送回客户端套接字时,它抛出异常,表示套接字已关闭。 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. 问题是,当我仅从服务器套接字输入流中读取时,它工作正常,但是当我也尝试从客户端套接字的输入流中读取时,它将引发异常。我知道我已经关闭了客户端套接字的bufferedOutputStream,但如果我不这样做,关闭它卡在服务器套接字的while循环中。 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). 不要关闭的OutputStream在客户端( bout.close(); )(至少不是调用之前Socket.shutdownOutput()见下文)。

From the Javadoc of Socket.getOutputStream() : Socket.getOutputStream()的Javadoc中:

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. 如果通道处于非阻塞模式,则输出流的写操作将引发java.nio.channels.IllegalBlockingModeException。

Closing the returned OutputStream will close the associated socket. 关闭返回的OutputStream将关闭关联的套接字。

If you want to indicate that you're done writing to the socket, call Socket.shutdownOutput() instead. 如果要表明已完成向套接字的写入,请改为调用Socket.shutdownOutput() It's Javadoc states: Javadoc指出:

Socket.shutdownOutput() : 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. 对于TCP套接字,将发送任何先前写入的数据,然后是TCP的正常连接终止序列。

After invoking Socket.shutdownOutput(), which waits for the data to be flushed, you can close the OutputStream , the InputStream , or the Socket . 调用等待刷新数据的Socket.shutdownOutput()之后,可以关闭OutputStreamInputStreamSocket (it doesn't matter which you close - all three will end up closing the Socket ) (不管关闭哪个,这三个都将最终关闭Socket

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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