c++的语句int iNumByte = sockSend.Send(&iEnd, sizeof(int));在C#中如何实现? .NET里面用System.Net.Sockets.Socket并且用法不一样,不要这样照搬 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 C#寫的簡單的FTP服務端。 private void btnStart_Click(object sender, System.EventArgs e) { try { port=Int32.Parse(textBox1.Text); } catch { MessageBox.Show("Port number wrong!"); } try { listener=new TcpListener(port); listener.Start(); statusBar1.Text="Service started!"; Thread thread=new Thread(new ThreadStart(recevive)); } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void recevive() { sock=listener.AcceptSocket(); if(sock.Connected) { statusBar1.Text="Connect succeed!"; string str=richTextBox1.Text; byte[] bytee=System.Text.Encoding.BigEndianUnicode.GetBytes(str.ToCharArray()); sock.Send(bytee,bytee.Length,0); while(!control) { NetworkStream stream=new NetworkStream(sock); byte[] by=new byte[1024]; int i=sock.Receive(by,by.Length,0); string ss=System.Text.Encoding.BigEndianUnicode.GetString(by); richTextBox2.AppendText(ss); j=richTextBox2.Lines.Length; if(j>=2) { if(richTextBox2.Lines[j-2].ToString()!="@@@@@@") { filestream=new FileStream(richTextBox2.Lines[j-2].ToString(),FileMode.Open,FileAccess.Read); byte[] bb=new byte[1024]; while((number=filestream.Read(bb,0,1024))!=0) { stream.Write(bb,0,number); stream.Flush(); } string st="<EOF>"; byte[] byt=new byte[1024]; byt=System.Text.Encoding.ASCII.GetBytes(st.ToCharArray()); sock.Send(byt,byt.Length,0); filestream.Close(); } else if(richTextBox2.Lines[j-2].ToString()=="@@@@@@") { control=true; } } } } } private void btnExit_Click(object sender, System.EventArgs e) { Application.Exit(); } private void btnStop_Click(object sender, System.EventArgs e) { try { control=true; listener.Stop(); statusBar1.Text="Service stop!"; } catch { MessageBox.Show("Service not start!"); } } 用connected属性似乎不妥!因为该属性反映的socket上次操作连接状态! dxjhq(毅楊) 的方法不能判断对方终止或线路断开的情况krank(Alex.Young) 说得对,connected属性是不实时的有好的办法的朋友来发表一下看法!!! C#怎么连接vpn 求助:WinForm中如何将Label的背景变成透明?? 简单购物车 如何判断datagrideview的列绑定的字段的类型 用label控件显示程序状态的问题 求助:动态生成控件,并使用其属性 如何自定义表格?同时控制各个表格的填写权限? 修改注册表问题? 百分求助,关于图文并茂的文本框的问题 高分求教,请问在WEB下怎样打印水晶报表。 棘手的问题?急!!! 请问:我载visio中的选择宋体时,将其复制到WORD中,文字怎么变成了?;请指点 谢谢
private void btnStart_Click(object sender, System.EventArgs e)
{
try
{
port=Int32.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("Port number wrong!");
} try
{
listener=new TcpListener(port);
listener.Start();
statusBar1.Text="Service started!";
Thread thread=new Thread(new ThreadStart(recevive));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void recevive()
{
sock=listener.AcceptSocket();
if(sock.Connected)
{
statusBar1.Text="Connect succeed!";
string str=richTextBox1.Text;
byte[] bytee=System.Text.Encoding.BigEndianUnicode.GetBytes(str.ToCharArray());
sock.Send(bytee,bytee.Length,0); while(!control)
{
NetworkStream stream=new NetworkStream(sock);
byte[] by=new byte[1024];
int i=sock.Receive(by,by.Length,0);
string ss=System.Text.Encoding.BigEndianUnicode.GetString(by); richTextBox2.AppendText(ss);
j=richTextBox2.Lines.Length;
if(j>=2)
{
if(richTextBox2.Lines[j-2].ToString()!="@@@@@@")
{
filestream=new FileStream(richTextBox2.Lines[j-2].ToString(),FileMode.Open,FileAccess.Read); byte[] bb=new byte[1024];
while((number=filestream.Read(bb,0,1024))!=0)
{
stream.Write(bb,0,number);
stream.Flush();
} string st="<EOF>";
byte[] byt=new byte[1024];
byt=System.Text.Encoding.ASCII.GetBytes(st.ToCharArray()); sock.Send(byt,byt.Length,0);
filestream.Close();
}
else if(richTextBox2.Lines[j-2].ToString()=="@@@@@@")
{
control=true;
}
}
}
} } private void btnExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
} private void btnStop_Click(object sender, System.EventArgs e)
{
try
{
control=true;
listener.Stop();
statusBar1.Text="Service stop!";
}
catch
{
MessageBox.Show("Service not start!");
}
}
krank(Alex.Young) 说得对,connected属性是不实时的
有好的办法的朋友来发表一下看法!!!