socket不能接受数据的问题 写了一段socket传输文件的代码但当客户端向服务器发送数据时报错:“由于套接字没有连接并且没有提供地址,发送或接受数据的请求没有被接受”但连接还是正常能连上在接受数据时就出了这个问题请大虾指点一下迷津 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先,请你确定你的client确实连上server 连接上之后还得为ClientSocket需要定义server端的IP地址和开放的端口(port) 代码如下客户端:Socket sok = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPEndPoint ippoint =new IPEndPoint(IPAddress.Parse("192.168.3.13"),8080); FileStream fs = new FileStream("D:\\Program\\实例代码\\Socket\\01.jpg",FileMode.OpenOrCreate,FileAccess.Read); byte[] fssize=new byte[fs.Length]; BinaryReader strread = new BinaryReader(fs); fs.Read(fssize, 0, fssize.Length-1); strread.Read(fssize, 0, fssize.Length-1); try { sok.Connect(ippoint); } catch { MessageBox.Show("服务未启动"); return; } sok.SendTo(fssize,ippoint); label1.Text = fs.Length.ToString(); fs.Close(); sok.Shutdown(SocketShutdown.Send); sok.Close();服务器Console.WriteLine("服务器启动!"); Socket sok = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPEndPoint ipend = new IPEndPoint(IPAddress.Parse("192.168.3.13"),8080); sok.Bind(ipend); sok.Listen(2); FileStream recfs = new FileStream("02.jpg",FileMode.OpenOrCreate); byte[] recbyte = new byte[229888]; sok.Accept(); BinaryWriter newfilestr = new BinaryWriter(recfs); sok.Receive(recbyte); recfs.Write(recbyte, 0, recbyte.Length-1); newfilestr.Write(recbyte, 0, recbyte.Length-1); recfs.Close(); sok.Shutdown(SocketShutdown.Receive); sok.Close(); Console.WriteLine("接受完毕"); Console.ReadLine(); 服务端的socket没有抓住这个connect 客户端 string ip_bj = Get_IP(); try { IPEndPoint hostEP=new IPEndPoint(IPAddress.Parse(textBox1.Text),int.Parse(textBox2.Text)); Socket mysocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); try { mysocket.Connect(hostEP); } catch(Exception ex) { MessageBox.Show("连接错误\n\r"+ex.Message); return; } byte[] msgx = Encoding.Default.GetBytes(ip_bj); try { mysocket.Send(msgx); } catch { MessageBox.Show("发送错误"); } byte[] bytes = new byte[1024]; int bytesRead=mysocket.Receive(bytes); listBox1.Items.Add(Encoding.Default.GetString(bytes,0,bytesRead) + ""+DateTime.Now+""); mysocket.Shutdown(SocketShutdown.Both); mysocket.Close(); } catch(Exception ex) { mysocket.Shutdown(SocketShutdown.Both); mysocket.Close(); }private string Get_IP() { IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; return addressList[0].ToString(); }服务端 public static IPAddress GetServer_IP() { IPHostEntry ipDns=Dns.GetHostByName(Dns.GetHostName()); return ipDns.AddressList[0]; } private void BeginListen() { IPAddress Server_Ip=GetServer_IP(); IPEndPoint ip=new IPEndPoint(Server_Ip,System.Convert.ToInt32(textBox1.Text.ToString())); Socket Socket_IP=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); Socket_IP.Bind(ip); while(true) { try { Socket_IP.Listen(5); Socket Socket_Listen=Socket_IP.Accept(); byte[] byteMsg=new byte[1024]; int msg = Socket_Listen.Receive(byteMsg); listBox1.Items.Add("Recive: " + Encoding.Default.GetString(byteMsg,0,msg) + " "+DateTime.Now+""); byte[] msgx = Encoding.Default.GetBytes(textBox2.Text); Socket_Listen.Send(msgx); } catch(SocketException ex) { MessageBox.Show(ex.Message); } } } 记得给分 服务器的代码:sok.Accept();//这里会返回一个Socket,你应该用这个socket做数据传输操作---->socket client = sok.Accept();//而sok可以继续监听:或者在client处理完后继续监听,或者分配一个线程进行client的处理,sok继续循环监听 C# 飞机游戏问题 Microsoft WEFT 制作 缅甸的eot字体的问题 数据库求助 请问各位大侠怎么把键盘上的“这个字符转换到文本框? 这个字符串如何截取? 怎样将日期转化为 February 28,2008 急需解决 求教本机进程间进行数据交换的方法? 读取系统日志的问题 实际应用中一个.NET remoting的问题 SQL 刚从VC/转到.Net下的C#,大家推荐一本书,谢过了!
客户端:Socket sok = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ippoint =new IPEndPoint(IPAddress.Parse("192.168.3.13"),8080);
FileStream fs = new FileStream("D:\\Program\\实例代码\\Socket\\01.jpg",FileMode.OpenOrCreate,FileAccess.Read);
byte[] fssize=new byte[fs.Length];
BinaryReader strread = new BinaryReader(fs);
fs.Read(fssize, 0, fssize.Length-1);
strread.Read(fssize, 0, fssize.Length-1);
try
{
sok.Connect(ippoint);
}
catch
{
MessageBox.Show("服务未启动");
return;
}
sok.SendTo(fssize,ippoint);
label1.Text = fs.Length.ToString();
fs.Close();
sok.Shutdown(SocketShutdown.Send);
sok.Close();
服务器
Console.WriteLine("服务器启动!");
Socket sok = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ipend = new IPEndPoint(IPAddress.Parse("192.168.3.13"),8080);
sok.Bind(ipend);
sok.Listen(2);
FileStream recfs = new FileStream("02.jpg",FileMode.OpenOrCreate);
byte[] recbyte = new byte[229888];
sok.Accept();
BinaryWriter newfilestr = new BinaryWriter(recfs);
sok.Receive(recbyte);
recfs.Write(recbyte, 0, recbyte.Length-1);
newfilestr.Write(recbyte, 0, recbyte.Length-1);
recfs.Close();
sok.Shutdown(SocketShutdown.Receive);
sok.Close();
Console.WriteLine("接受完毕");
Console.ReadLine();
string ip_bj = Get_IP();
try
{
IPEndPoint hostEP=new IPEndPoint(IPAddress.Parse(textBox1.Text),int.Parse(textBox2.Text));
Socket mysocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
mysocket.Connect(hostEP);
}
catch(Exception ex)
{
MessageBox.Show("连接错误\n\r"+ex.Message);
return;
}
byte[] msgx = Encoding.Default.GetBytes(ip_bj);
try
{
mysocket.Send(msgx);
}
catch
{
MessageBox.Show("发送错误");
}
byte[] bytes = new byte[1024];
int bytesRead=mysocket.Receive(bytes);
listBox1.Items.Add(Encoding.Default.GetString(bytes,0,bytesRead) + ""+DateTime.Now+"");
mysocket.Shutdown(SocketShutdown.Both);
mysocket.Close();
}
catch(Exception ex)
{
mysocket.Shutdown(SocketShutdown.Both);
mysocket.Close();
}private string Get_IP()
{
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
return addressList[0].ToString();
}
服务端 public static IPAddress GetServer_IP()
{ IPHostEntry ipDns=Dns.GetHostByName(Dns.GetHostName());
return ipDns.AddressList[0];
} private void BeginListen()
{
IPAddress Server_Ip=GetServer_IP();
IPEndPoint ip=new IPEndPoint(Server_Ip,System.Convert.ToInt32(textBox1.Text.ToString()));
Socket Socket_IP=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
Socket_IP.Bind(ip);
while(true)
{
try
{
Socket_IP.Listen(5);
Socket Socket_Listen=Socket_IP.Accept();
byte[] byteMsg=new byte[1024];
int msg = Socket_Listen.Receive(byteMsg);
listBox1.Items.Add("Recive: " + Encoding.Default.GetString(byteMsg,0,msg) + " "+DateTime.Now+"");
byte[] msgx = Encoding.Default.GetBytes(textBox2.Text);
Socket_Listen.Send(msgx);
}
catch(SocketException ex)
{
MessageBox.Show(ex.Message);
}
}
} 记得给分
sok.Accept();//这里会返回一个Socket,你应该用这个socket做数据传输操作---->socket client = sok.Accept();//而sok可以继续监听:或者在client处理完后继续监听,或者分配一个线程进行client的处理,sok继续循环监听