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];