异步套接字服务器再次收听

时间:2017-10-24 18:05:30

标签: android .net sockets asynchronous server

我正在使用Microsoft的Async Socket Listener中的示例。我让一切工作正常,我能够发送文件和数据等。但是,我正在努力保持套接字打开。我不想关闭套接字并为我发送的每个文件重新打开。我希望在发送所有文件之前保持打开状态。我认为这个问题出现在'SendCallback'中,但我似乎无法让它发挥作用。

.Net Code

 Private Shared Sub SendCallback(ar As IAsyncResult)
    Try
        ' Retrieve the socket from the state object.  
        Dim handler As Socket = DirectCast(ar.AsyncState, Socket)

        ' Complete sending the data to the remote device.  
        Dim bytesSent As Integer = handler.EndSend(ar)


        'trying to receive again - I added this after commenting out the below lines but am missing something.
        Dim state As StateObject = DirectCast(ar.AsyncState, StateObject)
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)


        'The below lines are what was there, I tried to comment out and use the above lines trying to receive again but it does not work. I assume I am missing something simple but can not find anything useful. Any help would be appreciated.

        'handler.Shutdown(SocketShutdown.Both)
        'handler.Close()
    Catch e As Exception
        PubVars.ServerStatus = e.ToString
    End Try
End Sub

我知道我不想关闭套接字直到发送完所有但我不确定我错过了什么。我想发送第一个文件,然后向客户端发送状态更新,这一切都运行良好。然后当客户端收到状态更新时,我想发送下一个文件,依此类推。

Android代码:

Socket socket = null;
        try {
            socket = new Socket("localhost", 11000);

            OutputStream out = socket.getOutputStream();
            PrintWriter output = new PrintWriter(out);

            String FileName = "my.jpg";
            String FilePath= Environment.getExternalStorageDirectory() + "/mydir/" + FileName;
            File f= new File(FilePath);
            byte[] data= readFileToByteArray(f);
            String strbase64 = Base64.encodeToString(data, Base64.DEFAULT);
            //I chose to pass the filename as part of my header string then parse it out on server
            output.println("<HEADER>" + FileName + "</HEADER>" + strbase64 + "<EOF>");
            output.flush();

            InputStream in = socket.getInputStream();

            BufferedReader r = new BufferedReader(new InputStreamReader(in));
            StringBuilder returnString = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                returnString.append(line).append('\n');
            }

            Log.i("INPUT",returnString.toString());
            //If true run the next one


            //SEND NEW FILE

            Log.i("NEXT","STARTING NEXT FILE");
            FileName = "my2.png";
            FilePath= Environment.getExternalStorageDirectory() + "/mydir/" + FileName;
            f= new File(FilePath);
            data= readFileToByteArray(f);
            strbase64 = Base64.encodeToString(data, Base64.DEFAULT);
            output.println("<HEADER>" + FileName + "</HEADER>" + strbase64 + "<EOF>");
            output.flush();

            in = socket.getInputStream();

            r = new BufferedReader(new InputStreamReader(in));
            returnString = new StringBuilder();

            while ((line = r.readLine()) != null) {
                returnString.append(line).append('\n');
            }

            Log.i("INPUT",returnString.toString());

            //END ANOTHER FILE




            output.close();
            in.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

0 个答案:

没有答案