socket 有问题求指教啊
wpf吧
全部回复
仅看楼主
level 3
直接贴代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SOCKET客户端
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private static byte[]result = new byte[1024];
private static string honst = "127.0.0.1";
private int port = 7800;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
IPAddress ip = IPAddress.Parse(honst);
Socket cliensocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
IPEndPoint ipe = new IPEndPoint(ip, port);
MessageBox.Show("连接服务器成功");
}
catch
{
MessageBox.Show("连接服务器失败");
return;
}
int revcievelength = cliensocket .Receive(result);
string sdf = Encoding.ASCII.GetString(result, 0, revcievelength);
Console.WriteLine("接收服务器消息:{0}", sdf);
tet.Text = "接受服务器消息:" + sdf;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
while(true)
{
Socket cliensocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
Thread.Sleep(1000);
string sendmsg = tet.Text + DateTime.Now.ToLongTimeString();
cliensocket.Send(Encoding.ASCII.GetBytes(sendmsg));
}
catch
{
cliensocket.Close();
break;
}
}
}
}
}
2015年10月12日 02点10分 1
level 3
这是异常消息。。 求指教呀
2015年10月12日 02点10分 2
1