Курсовая работа по ОС. Курсовая работа. исследование механизмов межпроцессного взаимодействия
Скачать 66.8 Kb.
|
КОД КЛИЕНТАФайл «Form1.cs» using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Sockets; using System.Threading; using System.Net; using System.Diagnostics; namespace ClientGUI { public partial class Form1 : Form { int port; static SocketClient client; public Form1() { InitializeComponent(); } private void connection () { string MyIp = ""; foreach (System.Net.IPAddress ip in System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList) { MyIp = ip.ToString(); break; } client = new SocketClient(MyIp, this.port); if (client.Connect()) { lbx_message.Invoke(new MethodInvoker(delegate { lbx_message.Items.Add("Соединение установлено"); })); Status(); } } private void btnCreateConnection_Click(object sender, EventArgs e) { port = 12000; Thread connect = new Thread(connection); connect.Start(); connect.IsBackground = true; } private void Status () { for (; ; ) { client.Receive(); try { if (client.ReceiveMessage[client.ReceiveMessage.Count-1] == "[end]") break; } catch { } } for (int i = 0; i < client.ReceiveMessage.Count; ) { if(client.ReceiveMessage[i]!="[end]") lbx_message.Invoke(new MethodInvoker(delegate { lbx_message.Items.Add(client.ReceiveMessage[i]); })); client.ReceiveMessage.RemoveAt(i); } } private void btnSendMessage_Click(object sender, EventArgs e) { this.port = 13000; Thread connect = new Thread(connection); connect.Start(); connect.IsBackground = true; } private void btnClear_Click(object sender, EventArgs e) { lbx_message.Items.Clear(); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } } Файл «SocketClient.cs» using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; using System.Threading; using System.Net; using System.Diagnostics; namespace ClientGUI { class SocketClient { public Socket client; private IPEndPoint ip; public List<string> SendMessage = new List<string>(); public List<string> ReceiveMessage = new List<string>(); public SocketClient(string ip, Int32 port) { this.ip = new IPEndPoint(IPAddress.Parse(ip), port); this.client = new Socket(this.ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } public bool Connect() { try { this.client.Connect(this.ip); return true; } catch { return false; } } public void Receive() { string message = String.Empty; byte[] GetBytes = new byte[1024]; try { int b = client.Receive(GetBytes); message = Encoding.UTF8.GetString(GetBytes, 0, b); if (message != "") this.ReceiveMessage.Add(message); } catch { } } public void Send(string message, Socket handler) { byte[] tosend = Encoding.UTF8.GetBytes(message); try { handler.Send(tosend, 0, tosend.Length, SocketFlags.None); } catch { } } public void Disconnect() { this.client.Disconnect(false); } }} |