server端 部分代码: 传送的是图片 ,但现实不了。
private void recieve()
{
port = Int32.Parse(textBox1.Text);
IPAddress myIP = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(myIP, port);
listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
string data="";
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
statusBar1.Text = "开始监听......";
handler = listener.Accept(); if (handler.Connected)
{
statusBar1.Text = "与客户建立连接";
} while (true)
{
byte[] bytes = new byte[1024];
int bytesrec = handler.Receive(bytes);
data += System.Text.Encoding.ASCII.GetString(bytes, 0, bytesrec);
if (data.IndexOf("<EOF>")>-1)
{
break;
} }
//MessageBox.Show(data.ToString());
int lenn = "<EOF>".Length;
int lenn1 = data.Length - lenn;
string ss = data.Substring(0, lenn1);
byte[] by = System.Text.Encoding.ASCII.GetBytes(ss);
MessageBox.Show((by.Length+"fwq").ToString());
MemoryStream ms = new MemoryStream(by);
this.pictureBox1.Image = Image.FromStream(ms);//《==参数无效
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
client 端的主要代码:
private void read()
{
int port = 0;
port = Int32.Parse(this.textBox2.Text.ToString());
IPAddress myIP = IPAddress.Parse("127.0.0.1"); myIP = IPAddress.Parse(textBox1.Text);
IPEndPoint remoteEP = new IPEndPoint(myIP, port);
soc = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
soc.Connect(remoteEP); string file = @"f:\tupian\1.jpg";
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
int len =(int) fs.Length;
byte[] buf = new byte[len];
fs.Read(buf, 0, len);
string sr = System.Text.Encoding.ASCII.GetString(buf);
//byte[] msg = System.Text.Encoding.ASCII.GetBytes("this is a test<EOF>");
string s = sr + "<EOF>";
byte[] b = System.Text.Encoding.ASCII.GetBytes(s);
//MessageBox.Show("<EOF>".Length.ToString());
MessageBox.Show((b.Length + "khd").ToString());
int bytesent = soc.Send(b);
soc.Shutdown(SocketShutdown.Both);
soc.Close();
}
catch(ArgumentNullException ane)
{
MessageBox.Show(ane.ToString()); } }
}
private void recieve()
{
port = Int32.Parse(textBox1.Text);
IPAddress myIP = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(myIP, port);
listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
string data="";
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
statusBar1.Text = "开始监听......";
handler = listener.Accept(); if (handler.Connected)
{
statusBar1.Text = "与客户建立连接";
} while (true)
{
byte[] bytes = new byte[1024];
int bytesrec = handler.Receive(bytes);
data += System.Text.Encoding.ASCII.GetString(bytes, 0, bytesrec);
if (data.IndexOf("<EOF>")>-1)
{
break;
} }
//MessageBox.Show(data.ToString());
int lenn = "<EOF>".Length;
int lenn1 = data.Length - lenn;
string ss = data.Substring(0, lenn1);
byte[] by = System.Text.Encoding.ASCII.GetBytes(ss);
MessageBox.Show((by.Length+"fwq").ToString());
MemoryStream ms = new MemoryStream(by);
this.pictureBox1.Image = Image.FromStream(ms);//《==参数无效
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
client 端的主要代码:
private void read()
{
int port = 0;
port = Int32.Parse(this.textBox2.Text.ToString());
IPAddress myIP = IPAddress.Parse("127.0.0.1"); myIP = IPAddress.Parse(textBox1.Text);
IPEndPoint remoteEP = new IPEndPoint(myIP, port);
soc = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
soc.Connect(remoteEP); string file = @"f:\tupian\1.jpg";
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
int len =(int) fs.Length;
byte[] buf = new byte[len];
fs.Read(buf, 0, len);
string sr = System.Text.Encoding.ASCII.GetString(buf);
//byte[] msg = System.Text.Encoding.ASCII.GetBytes("this is a test<EOF>");
string s = sr + "<EOF>";
byte[] b = System.Text.Encoding.ASCII.GetBytes(s);
//MessageBox.Show("<EOF>".Length.ToString());
MessageBox.Show((b.Length + "khd").ToString());
int bytesent = soc.Send(b);
soc.Shutdown(SocketShutdown.Both);
soc.Close();
}
catch(ArgumentNullException ane)
{
MessageBox.Show(ane.ToString()); } }
}
解决方案 »
- LABEL显示 隐藏 闪烁
- 项目寻求合作,有能力现金交易
- 如何把c#中的变量传道前面javascript里
- 简单而头大的问题:WINFORM下输入法无法切换的问题
- C#进行window开发,如何连接informix数据库
- 这个基类应该怎样定义,谢谢大家
- 读取文本到textbox后,这么使光标自动移动到文件末尾
- myclient = new TcpClient("127.0.0.1", Int32.Parse(labelPort.Text));这句有错吗?
- SendMessage 发送不成功,请帮下,谢谢
- 我今天装了vs.net bete2 中文版,但是总是提示“找不到文件或程序集名称system”
- 线程并发问题?
- 问一个比较难的问题,当然大家不会可以一起来讨论下.
我看了 传输的和接受的大小一样。难道图片不能传输字符串?