Android 2.2 전화에서 Java Android App으로 문자열 데이터를 C# 프로그램으로 보냅니다. 데이터를 수신하고 올바르게 표시하는 C# 프로그램 처음으로. 그런 다음 데이터 수신이 중지되지 않습니다. 그러나 데이터가 없으므로 Java App에서 두 번째로 보낸 데이터를 수신하지 않고 디버깅하는 동안 수신 된 데이터로 0
으로 표시됩니다.TCP 소켓이 C# 프로그램에서 데이터 수신을 중지하지 않습니다.
처음에는 자바 앱이 계속 데이터를 보내고 있다고 생각했습니다. 그러나 C# 프로그램은 Java App이 종료 된 경우에도 데이터를 수신합니다. C# 프로그램의
전체 소스 코드에서 다음과 같습니다 :
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
namespace Network_IM
{
public partial class Server : Form
{
public Socket listnerSocket;
public Socket workerSocket;
public AsyncCallback workerAsyncCallBack;
//As a Client
Socket clientSocket;
public Server()
{
InitializeComponent();
OnLoad();
RegisterEvents();
}
private void RegisterEvents()
{
button1.Click += new EventHandler(button1_Click);
txtInput.KeyDown += new KeyEventHandler(txtInput_KeyDown);
}
void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
button1_Click(null, null);
}
void button1_Click(object sender, EventArgs e)
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(IPAddress.Parse("192.168.1.5"), 8222);
clientSocket.Send(Encoding.UTF8.GetBytes(txtInput.Text));
clientSocket.Close();
txtLog.Text += " | Me: " + txtInput.Text + " | ";
txtInput.Clear();
txtInput.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void OnLoad()
{
try
{
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8221);
listnerSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
listnerSocket.Bind(ipLocal);
listnerSocket.Listen(4);
listnerSocket.BeginAccept(new AsyncCallback(OnClientConnect),
null);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public void OnClientConnect(IAsyncResult asyn)
{
try
{
workerSocket = listnerSocket.EndAccept(asyn);
WaitForData(workerSocket);
}
catch (ObjectDisposedException)
{
Debugger.Log(0, "1", "\n OnClientConnection: Socket has been
closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void WaitForData(Socket workerSoc)
{
try
{
if (workerAsyncCallBack == null)
workerAsyncCallBack = new AsyncCallback(OnDataReceived);
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = workerSoc;
workerSoc.BeginReceive(theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Length, SocketFlags.None, workerAsyncCallBack, theSocPkt);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public void OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
int iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
Encoding.UTF8.GetDecoder().GetChars(theSockId.dataBuffer, 0, iRx,
chars, 0);
String szData = new String(chars);
setTxtLogText(szData);
WaitForData(workerSocket);
}
catch (ObjectDisposedException)
{
Debugger.Log(0, "1", "\nOnDataReceived: Socket has been
closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
delegate void setTxtLogTextDelegate(string newText);
private void setTxtLogText(string newText)
{
try
{
if (txtLog.InvokeRequired)
{
setTxtLogTextDelegate txtLogDelegate = new
setTxtLogTextDelegate(setTxtLogText);
if (newText != "\0")
txtLog.Invoke(txtLogDelegate, new object[] { newText });
}
else
txtLog.Text += newText.Remove(1);
}
catch (Exception ex)
{ throw ex; }
}
}
public class CSocketPacket
{
public Socket thisSocket;
public byte[] dataBuffer = new byte[1];
}
}
내가 또 다시 소스 코드를 통과 미친 얻고있다. 도와주세요. :)
감사합니다. @Marc. 어떻게 내가 덤프. 나는 이처럼 간단하게 생각조차 할 수 없다. 나는 또한 [CodeProject] (http://www.codeproject.com/Questions/339610/TCP-Socket-is-not-stopping-receiving-data-in-Cshar)에이 질문을 게시했다. [여기에있는 대답] (http://www.codeproject.com/Answers/339849/TCP-Socket-is-not-stopping-receiving-data-in-Cshar)도 나에게 많은 도움이되었습니다. 대단히 감사합니다. – Krish