关于Socket.Receive阻塞问题? - .NET技术 / 非技术区
C#与JAVA通信
下面是自定义WebControls控件中的主要代码
//===============================================================
protected virtual void OnSubmit(EventArgs e){
int port = 8896;
IPAddress ip = IPAddress.Parse( "127.0.0.1 ");
IPEndPoint hostEP = new IPEndPoint(ip, port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
TextBox sBox = ((TextBox)this.Parent.FindControl( "sTextBox "));
try{
sBox.Text = "开始连接 ";
socket.ReceiveTimeout = 10000;
socket.Connect(hostEP);
sBox.Text = "连接成功 ";
}catch(Exception se){
((TextBox)this.Parent.FindControl( "sTextBox ")).Text = "连接出错 ";
}
//发送给远程主机的请求内容串
string sendStr = "123456;abcd ";
byte[] bytesSendStr = new byte[1024];
bytesSendStr = System.Text.Encoding.ASCII.GetBytes(sendStr);
//向主机发送请求
try{
socket.Send(bytesSendStr,bytesSendStr.Length,0);
}catch(Exception ce){
sBox.Text = "发送数据出错 ";
}
//声明接收返回内容的字符串
byte[] recvBytes = new byte[1024];
//返回实际接收内容的字节数
int bytes = 0;
//循环读取,直到接收完所有数据
while (true){
bytes = socket.Receive(recvBytes, recvBytes.Length, 0);
//读取完成后退出循环
if (bytes > 0)
break;
}
if (recvBytes[0] == 1)
sBox.Text = "true ";
else
sBox.Text = "false ";
socket.Shutdown(SocketShutdown.Both);
socket.Close();
//----------------------------------------------
EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
if (SubmitHandler != null){
SubmitHandler(this, e);
}
}
//============================================================
下面是JAVA的Server端的主要代码:
try {
InputStream in = socket.getSocket().getInputStream();
int bytesRead;
byte[] buffer = new byte[100];
String data = " ";
while ((bytesRead = in.read(buffer)) != -1) {
data = new String(buffer, 0, bytesRead);
}
System.err.println(data);
socket.getSocket().getOutputStream().write(1);
} catch (Exception e) {
}
[解决办法]
什么问题?
------解决方案--------------------
呵呵
[解决办法]
来晚一步,只能接分了!