当我的Android客户端连接到服务器(用C#编写)时,服务器将收到一系列'\ 0'。即使我关闭Android应用程序或断开手机,服务器仍会收到此数据。
我使用C#客户端测试了我的服务器,服务器上没有问题。
这是我在C#中的服务器代码:
private void listen_Btn_Click(object sender, EventArgs e)
{
try
{
_socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
// Bind to local IP Address...
_socket.Bind(ipLocal);
// Start listening...
_socket.Listen(4);
// Create the call back for any client connections...
_socket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
// This is the call back function, which will be invoked when a client is connected
public void OnClientConnect(IAsyncResult asyn)
{
try
{
Socket workerSocket = _socket.EndAccept(asyn);
_socket.BeginAccept(new AsyncCallback(OnClientConnect), null);
Interlocked.Increment(ref m_clientCount);
m_workerSocketList.Add(workerSocket);
clientsName.Add("$unknown$");
if (listBox1.InvokeRequired)
{
listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Clear(); foreach (string name in clientsName) listBox1.Items.Add(name); }));
}
ArrayList rthreadParams = new ArrayList();
rthreadParams.Add(workerSocket);
rthreadParams.Add(m_clientCount);
Thread rThread = new Thread(new ParameterizedThreadStart(WaitForData));
rThread.Start(rthreadParams);
rThread.Join();
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public class SocketPacket
{
public Socket m_currentSocket;
public int m_clientNumber;
public SocketPacket(Socket socket, int clientNumber)
{
m_currentSocket = socket;
m_clientNumber = clientNumber;
}
public byte[] dataBuffer = new byte[1024];
}
public void WaitForData(object param)
{
try
{
Socket soc = (Socket)((ArrayList)param)[0];
int clientNumber = (int)((ArrayList)param)[1];
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket(soc, clientNumber);
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length,
SocketFlags.None, pfnWorkerCallBack, theSocPkt);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public void OnDataReceived(IAsyncResult asyn)
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
try
{
int iRx = socketData.m_currentSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
String szData = new String(chars);
szData = szData.Replace("\0", "");
// doing some work with szData
ArrayList t_Params = new ArrayList();
t_Params.Add(socketData.m_currentSocket);
t_Params.Add(socketData.m_clientNumber);
WaitForData(t_Params);
}
catch (Exception ee)
{
}
}
这是Android客户端代码:
try
{
InetSocketAddress sa = new InetSocketAddress("192.168.50.11", 2001);
Socket socket = new Socket();
socket.connect(sa,4000);
OutputStream dos = socket.getOutputStream();
PrintWriter out = new PrintWriter(dos);
out.write("hello");
out.flush();
out.close();
socket.close();
}
catch (Exception e)
{
Log.d(appTag, e.toString());
}
我该如何解决这个问题?
答案 0 :(得分:0)
我使用以下代码在python中创建了另一个客户端:
import socket
s = socket.socket(socket.AF_INET,socket.SOC_STREAM)
s.connect(("127.0.0.1",2001))
s.send("hello!")
并且在服务器中没有这样的问题。 只有当我在android中创建一个套接字时,才会出现这个问题。
问题在哪里?