C#学习——简单socket、多线程
2016-04-25 11:46
615 查看
1、C#socket
using System.Net;
using System.Net.Sockets;
using System.Threading;
public string Connect_Server(string IP, string Port, out int re)
{
IPAddress ip = IPAddress.Parse(IP);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSocket.Connect(new IPEndPoint(ip, Int32.Parse(Port)));
re = 0;
return "Connect Success";
}
catch
{
re = 1;
return "Connect Fail";
}
}
public string Receive()
{
byte[] result = new byte[1024];
clientSocket.Receive(result);
return System.Text.Encoding.Default.GetString(result);
}
public void SendData(string data)
{
clientSocket.Send(Encoding.ASCII.GetBytes(data));
}
2、多线程
using System.Threading;
Control.CheckForIllegalCrossThreadCalls = false; //c#控件是非线程安全的,多线程设置控件属性的时候,调试状态会报错InvalidOperationException,添加此语句可以忽略此错误
//线程启动
Thread thread_rec thread_rec = new Thread(new ThreadStart(ThreadFunc));
thread_rec.Start();
//多线程中启动窗体
private void ThreadFunc()
{
MethodInvoker mi = new MethodInvoker(this.Wait_Receive);
this.BeginInvoke(mi);
}
public void Wait_Receive()
{
while (true)
{
//
}
Login login = new Login(re_socket);
login.Show();
this.Hide();
}
3.定时器
System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(Tick), null, 3000, 3000);
void Tick(object data)
{
if (!receive_sig)
{
label_connectInfo.Text = "receive string timeout";
button_connect.Enabled = true;
receive_sig = true;
Application.DoEvents();
thread_rec.Interrupt();
}
timer.Dispose();
}
using System.Net;
using System.Net.Sockets;
using System.Threading;
public string Connect_Server(string IP, string Port, out int re)
{
IPAddress ip = IPAddress.Parse(IP);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSocket.Connect(new IPEndPoint(ip, Int32.Parse(Port)));
re = 0;
return "Connect Success";
}
catch
{
re = 1;
return "Connect Fail";
}
}
public string Receive()
{
byte[] result = new byte[1024];
clientSocket.Receive(result);
return System.Text.Encoding.Default.GetString(result);
}
public void SendData(string data)
{
clientSocket.Send(Encoding.ASCII.GetBytes(data));
}
2、多线程
using System.Threading;
Control.CheckForIllegalCrossThreadCalls = false; //c#控件是非线程安全的,多线程设置控件属性的时候,调试状态会报错InvalidOperationException,添加此语句可以忽略此错误
//线程启动
Thread thread_rec thread_rec = new Thread(new ThreadStart(ThreadFunc));
thread_rec.Start();
//多线程中启动窗体
private void ThreadFunc()
{
MethodInvoker mi = new MethodInvoker(this.Wait_Receive);
this.BeginInvoke(mi);
}
public void Wait_Receive()
{
while (true)
{
//
}
Login login = new Login(re_socket);
login.Show();
this.Hide();
}
3.定时器
System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(Tick), null, 3000, 3000);
void Tick(object data)
{
if (!receive_sig)
{
label_connectInfo.Text = "receive string timeout";
button_connect.Enabled = true;
receive_sig = true;
Application.DoEvents();
thread_rec.Interrupt();
}
timer.Dispose();
}
相关文章推荐
- C#日期时间格式化
- Webdriver(selenium2.0)+NUnit+C# (一)
- C#新手学习快速成长学习资源汇总
- C#下批量判断该条记录在表中是否存在问题
- VS快捷键
- C#中params关键字的作用理解
- 在C#里面给PPT添加注释
- 【转】C#中virtual和abstract的区别
- C# Bulk Operations
- C#CreateGraphics方法的三种实现方式
- C# 去除字符串里的重复字符
- C# 开源(name)
- C#_会员管理系统:开发七(用户分类)
- c# foreach 和for循环比较
- Learning hard C#学习笔记
- c#使用MethodInvoker解决跨线程访问控件
- C#学习笔记---函数 的理解及总结(入门级)
- 进程(应用程序)之二
- C# MessageBox常用用法
- C# 正则表达式匹配汉字