Code C lập trình mạng bằng Socket UDP và TCP

9 33 0
  • Loading ...
1/9 trang

Thông tin tài liệu

Ngày đăng: 11/06/2018, 09:25

TCP- GUI NHẬN BẰNG STRING SERVER NHÉ CHÓ 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.IO; namespace server { class Program { static bool kttang(int[] a, int n) { for (int i = 0; i < n-1; i++) { if (a[i] > a[i + 1]) return false; } return true; } static bool xuathien(int[] a, int n, int x) { for (int i = 0; i < n; i++) if (a[i] == x) return true; return false; } static bool songuyento(int[] a, int n) { if (n max) max = a[i]; } return max; } static int min(int[] a, int n) { int = a[0]; for (int i = 1; i < n; i++) { if (a[i] < min) = a[i]; } return min; } static void Main(string[] args) { IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2018); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(ipe); server.Listen(10); Console.WriteLine("Waitting "); Socket client = server.Accept(); Console.WriteLine("Chap nhan ket noi tu: " + client.RemoteEndPoint.ToString()); string s = "Server chao ban!"; NetworkStream ns = new NetworkStream(client); StreamReader sr = new StreamReader(ns); StreamWriter sw = new StreamWriter(ns); // sw.WriteLine(s_gui); sw.WriteLine(s); sw.Flush(); while (true) { s= sr.ReadLine(); if (s == "exit") break; Console.WriteLine("Client: " + s); while (s.IndexOf(" ") != -1) s = s.Replace(" ", " "); s=s.Trim(); string[] str = s.Split(' '); int n = str.Length; int[] a = new int[100]; try { for (int i = 0; i < str.Length; i++) { a[i] = int.Parse(str[i]); } } catch { sw.WriteLine("sai dinh dang"); sw.Flush(); } string s5; if (kttang(a, n)) s5="server: chuoi vua nhap tang dan"; else s5="server: chuoi vua nhap khong tang dan"; sw.WriteLine(s5); sw.Flush(); int x = int.Parse(sr.ReadLine()); //string s2 = " "; string s4; if (xuathien(a, n, x)) { s4 = "cac vi tri xuat hien cua " + x.ToString() + " la:"; for (int i = 0; i < n; i++) { if (a[i] == x) { s4 = s4 + (i + 1).ToString() + " "; } } } else s4 = "khong xuat hien day."; sw.WriteLine(s4); sw.Flush(); //Console.ReadKey(); //string s3 = " "; s4 = "cac so chinh phuong mang la:"; for (int i = 0; i < n; i++) if (sochinhphuong(a[i], n)) { s4 = s4 + a[i].ToString() + " "; } sw.WriteLine(s4); sw.Flush(); //string s4 = " "; s4 = "so lon nhat day:" + max(a, n).ToString(); sw.WriteLine(s4); sw.Flush(); //string s4 = " "; s4 = "so nho nhat day:" + min(a, n).ToString(); sw.WriteLine(s4); sw.Flush(); for (int i = 0; i < n - 1; i++) { if (a[i] % != 0) { for (int j = i + 1; j < n; j++) { if (a[j] % != 0) { if (a[i] >= a[j]) { int tg = a[i]; a[i] = a[j]; a[j] = tg; } } } } } s4 = "chuoi sap xep le tang dan la: "; for (int i = 0; i < n; i++) { s4 = s4 + a[i].ToString() + " "; } sw.WriteLine(s4); sw.Flush(); } } } ns.Close(); sw.Close(); sr.Close(); client.Close(); server.Close(); } CLIENT NHÉ CHÓ 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.IO; namespace client { class Program { static void Main(string[] args) { IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2018); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(ipe); //NetworkStream: luong de truyen du lieu tren mang NetworkStream ns = new NetworkStream(client); //StreamReader su dung de nhan du lieu StreamReader sr = new StreamReader(ns); //StreamReader su dung de gui du lieu StreamWriter sw = new StreamWriter(ns); while (true) { //Nhan mess tu server //byte[] data_nhan = new byte[1024]; //int dembyte = client.Receive(data_nhan); //String s = Encoding.ASCII.GetString(data_nhan, 0, dembyte); String s = sr.ReadLine(); //String s = sr.ReadLine(); Console.WriteLine("Server: "+s); //Gui mess sang Server s = Console.ReadLine(); sw.WriteLine(s); sw.Flush(); //byte[] data_gui = new byte[1024]; //data_gui = Encoding.ASCII.GetBytes(s); //client.Send(data_gui, data_gui.Length, SocketFlags.None); if (s == "exit") break; string s5 = sr.ReadLine(); Console.WriteLine(" " + s5); s = Console.ReadLine(); sw.WriteLine(s); sw.Flush(); string s4 = sr.ReadLine(); Console.WriteLine(" " + s4); s4 = sr.ReadLine(); Console.WriteLine(" " + s4); s4 = sr.ReadLine(); Console.WriteLine(" " + s4); s4 = sr.ReadLine(); Console.WriteLine(" " + s4); s4 = sr.ReadLine(); Console.WriteLine(" " + s4); } client.Dispose(); //ns.Close(); //sw.Close(); //sr.Close(); client.Close(); } } } UDP- GỦI NHẬN THƠNG QUA BYTE SERVER NHÉ CHĨ 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.IO; namespace server { class Program { static bool kttang(int []a,int n) { for(int i=0;i a[i + 1]) return false ; } return true; } static bool xuathien(int []a,int x,int n) { for (int i = 0; i < n; i++) { if (a[i] ==x) return true; } return false; } static bool snt(int n) { for (int i = 2; i < n; i++) { if (n%i==0) return false ; } return true; } static int max(int []a,int n) { int max=a[0]; for (int i = 0; i < n; i++) { if (a[i] >= max) max = a[i]; } return max; } static void Main(string[] args) { IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2019); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); server.Bind(ipe); IPEndPoint ipenew = new IPEndPoint(IPAddress.Any, 0); EndPoint ep = (EndPoint)ipenew; //nhan byte[] nhan = new byte[1024]; int demnhan = server.ReceiveFrom(nhan, ref ep); string snhan = Encoding.ASCII.GetString(nhan, 0, demnhan); Console.WriteLine("client :" + snhan); //gui byte[] gui = new byte[1024]; string sgui = "chao client, t la server"; gui = Encoding.ASCII.GetBytes(sgui); server.SendTo(gui, ep); while (true ) { nhan = new byte[1024]; demnhan = server.ReceiveFrom(nhan, ref ep); snhan = Encoding.ASCII.GetString(nhan, 0, demnhan); while (snhan.IndexOf(" ") != -1) snhan = snhan.Replace(" ", " "); snhan.Trim(); string[] str = snhan.Split(' '); int n = str.Length; int[] a = new int[n]; for (int i = 0; i < str.Length; i++) { a[i] = int.Parse(str[i]); } string kq; if(kttang (a,n)) kq="day duoc sap xep"; else kq="day chua duoc sap xep"; gui = new byte[1024]; gui = Encoding.ASCII.GetBytes(kq); server.SendTo(gui, ep); //gui x nhan = new byte[1024]; demnhan = server.ReceiveFrom(nhan , ref ep); snhan = Encoding.ASCII.GetString(nhan, 0, demnhan); int x = int.Parse(snhan); if (xuathien(a, x, n)) { kq = "co xuat hien " + x.ToString() + " o vi tri : "; for (int i = 0; i < n; i++) { if (a[i] == x) { kq = kq + (i + 1).ToString() + " "; } } } else kq = "khong xuat hien"; gui = new byte[1024]; gui = Encoding.ASCII.GetBytes(kq); server.SendTo(gui, ep); kq = "so lon nhat: " + max(a, n).ToString(); gui = new byte[1024]; gui = Encoding.ASCII.GetBytes(kq); server.SendTo(gui, ep); kq = "cso nguyen to la : "; for (int i = 0; i < n; i++) { if (snt (a[i])) { kq = kq + a[i].ToString () + " "; } } kq = "mang sau sap xep la:"; for(int i=0;i
- Xem thêm -

Xem thêm: Code C lập trình mạng bằng Socket UDP và TCP, Code C lập trình mạng bằng Socket UDP và TCP

Gợi ý tài liệu liên quan cho bạn

Nhận lời giải ngay chưa đến 10 phút Đăng bài tập ngay