使用 TCP/IP 方式:
Client 端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication_TCPclient
{
class Program
{
static void Main(string[] args)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.100.1"), 20);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Connect(ipep);
int j = 0;
while(true)
{
j++;
string input = "Hello word !! \r\n";
byte[] data = Encoding.UTF8.GetBytes(input);
server.Send(data);
for (int h = 0; h < 100000; h++) ;
if (j > 10) break;
}
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
}
Server端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleApplicationTcpServer
{
class Program
{
static void Main(string[] args)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 20);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
while(true)
{
Socket client = newsock.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Client End Point= " + clientep);
TcpListener listener = new TcpListener(client);
Thread thread = new Thread(new ThreadStart(listener.run));
thread.Start();
}
}
}
public class TcpListener
{
Socket socket;
public TcpListener(Socket s)
{
socket = s;
}
public void run()
{
try
{
while(true)
{
byte[] data = new byte[1024];
int recv = socket.Receive(data);
if (recv == 0) break;
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
}
socket.Close();
} catch(Exception e)
{
Console.WriteLine("Client " + socket.RemoteEndPoint + " Error:close");
Console.WriteLine(e);
}
}
}
}
成功 ^^
另一個程式;
Server 端:
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections; //匯入集合物體功能
namespace WindowsFormsApplication_TCP
{
public partial class Form1 : Form
{
TcpListener server; //電話總機
Socket Client; //電話分機
Thread Th_svr; //電話總機開放中
Thread Th_Clt; //電話分機連線中
Hashtable HT = new Hashtable(); //客戶名稱與通訊物件的集合(雜湊表)(Key:Name:Socket)
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//忽略跨執行緒處理的錯誤(允許跨執行緒存許變數)
CheckForIllegalCrossThreadCalls = false;
Th_svr = new Thread(ServerSub);
Th_svr.IsBackground = true;
Th_svr.Start();
button1.Enabled = false;
}
private void ServerSub()
{
//Server IP and Port
IPEndPoint EP = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
server = new TcpListener(EP); //建立伺服端監聽器(總機)
server.Start(100); //啟動監聽設定,允許最多連線數100人
while(true)
{
Client = server.AcceptSocket(); //建立此客戶的連線物件Client
Th_Clt = new Thread(Listen); //建立監聽這個客戶連線的獨立執行緒
Th_Clt.IsBackground = true;
Th_Clt.Start();
}
}
private void Listen()
{
Socket sck = Client;
Thread Th = Th_Clt;
while(true)
{
try
{
byte[] B = new byte[1023];
int inLen = sck.Receive(B);
String Msg = Encoding.Default.GetString(B, 0, inLen);
String Cmd = Msg.Substring(0, 1);
String Str = Msg.Substring(1);
switch(Cmd)
{
case "0":
HT.Add(Str, sck);
listBox1.Items.Add(Str);
break;
case "9":
HT.Remove(Str);
listBox1.Items.Remove(Str);
Th.Abort();
break;
}
}
catch(Exception)
{
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.ExitThread();
}
}
}
執行結果
Client 端:
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplicationTCP_Client
{
public partial class Form1 : Form
{
Socket T;
String User;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string IP = textBox1.Text;
int Port = int.Parse(textBox2.Text);
IPEndPoint EP = new IPEndPoint(IPAddress.Parse(IP), Port);
T = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
User = textBox3.Text;
try
{
T.Connect(EP);
Send("0" + User);
}
catch(Exception)
{
MessageBox.Show("無法連上伺服器");
return;
}
button1.Enabled = false;
}
private void Send(String str)
{
byte[] B = Encoding.Default.GetBytes(str);
T.Send(B, 0, B.Length, SocketFlags.None); //使用連線物件傳送資烙
}
}
}
執行結果:
沒有連線過,不知是否有問題....
另一個程式;
Server 端:
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections; //匯入集合物體功能
namespace WindowsFormsApplication_TCP
{
public partial class Form1 : Form
{
TcpListener server; //電話總機
Socket Client; //電話分機
Thread Th_svr; //電話總機開放中
Thread Th_Clt; //電話分機連線中
Hashtable HT = new Hashtable(); //客戶名稱與通訊物件的集合(雜湊表)(Key:Name:Socket)
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//忽略跨執行緒處理的錯誤(允許跨執行緒存許變數)
CheckForIllegalCrossThreadCalls = false;
Th_svr = new Thread(ServerSub);
Th_svr.IsBackground = true;
Th_svr.Start();
button1.Enabled = false;
}
private void ServerSub()
{
//Server IP and Port
IPEndPoint EP = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
server = new TcpListener(EP); //建立伺服端監聽器(總機)
server.Start(100); //啟動監聽設定,允許最多連線數100人
while(true)
{
Client = server.AcceptSocket(); //建立此客戶的連線物件Client
Th_Clt = new Thread(Listen); //建立監聽這個客戶連線的獨立執行緒
Th_Clt.IsBackground = true;
Th_Clt.Start();
}
}
private void Listen()
{
Socket sck = Client;
Thread Th = Th_Clt;
while(true)
{
try
{
byte[] B = new byte[1023];
int inLen = sck.Receive(B);
String Msg = Encoding.Default.GetString(B, 0, inLen);
String Cmd = Msg.Substring(0, 1);
String Str = Msg.Substring(1);
switch(Cmd)
{
case "0":
HT.Add(Str, sck);
listBox1.Items.Add(Str);
break;
case "9":
HT.Remove(Str);
listBox1.Items.Remove(Str);
Th.Abort();
break;
}
}
catch(Exception)
{
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.ExitThread();
}
}
}
Client 端:
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplicationTCP_Client
{
public partial class Form1 : Form
{
Socket T;
String User;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string IP = textBox1.Text;
int Port = int.Parse(textBox2.Text);
IPEndPoint EP = new IPEndPoint(IPAddress.Parse(IP), Port);
T = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
User = textBox3.Text;
try
{
T.Connect(EP);
Send("0" + User);
}
catch(Exception)
{
MessageBox.Show("無法連上伺服器");
return;
}
button1.Enabled = false;
}
private void Send(String str)
{
byte[] B = Encoding.Default.GetBytes(str);
T.Send(B, 0, B.Length, SocketFlags.None); //使用連線物件傳送資烙
}
}
}
沒有連線過,不知是否有問題....
沒有留言:
張貼留言