Client Socket Server Socket Bind Connect Con nec
Client ﺗﻌﺎﻣﻞ Socket Server Socket Bind Connect Con nec t. M Ack sg ms g Listen Accept Send/Receive Close
Socket(Address. Family af, Socket. Type st, Protocol. Type pt) ��� �� ���� : Address. Family . ﺍﺳﺘﻔﺎﺩﻩ کﻨیﻢ Address. Family. Inter. Work ����� �� ����� IP ���� ���� ������ : Socket. Type Dgram UDP � Stream Raw TCP ICMP � � �� ������ : Protocol. Type
ﺑﺮﻧﺎﻣﻪ ﺳﻤﺖ کﺎﺭﺑﺮ byte[] data = new byte [2048]; string input, string. Data; IPEnd. Point ipep = new IPEnd. Point (IPAddress. Parse("127. 0. 0. 1"), 5060); Socket server = new Socket(Address. Family. Inter. Network, Socket. Type. Stream, Protocol. Type. Tcp); server. Connect(ipep); int recv = server. Receive(data); string. Data = Encoding. ASCII. Get. String(data, 0, recv); text. Box 1. Text = string. Data;
ﺑﺮﻧﺎﻣﻪ ﺳﻤﺖ ﺳﺮﻭﺭ IPAddress ip; IPEnd. Point ie; byte[] data = new byte[2048]; ip = IPAddress. Parse("127. 0. 0. 1"); ie=new IPEnd. Point(ip, 5060); Socket s=new Socket(Address. Family. Inter. Network, Socket. Type. Stream, Protocol. Type. Tcp); s. Bind(ie); s. Listen(6); Socket client = s. Accept(); IPEnd. Point client. Ep = (IPEnd. Point)client. Remote. End. Point; string welcome = "Welcome to my test server"; data = Encoding. ASCII. Get. Bytes(welcome); client. Send(data, data. Length, Socket. Flags. None);
ﺗﻮﺍﺑﻊ ﺑﺮ ﺍﺳﺎﺱ : System. Net. Sockets. Tcp. Listener ﺍیﺠﺎﺩ ﺷﻤﺎﺭﻩ پﻮﺭﺕ Tcp. Listener listener = new Tcp. Listener(local_port); ﺷﺮﻭﻉ ﺍﻧﺘﻈﺎﺭ ﺑﺮﺍی ﺍﺗﺼﺎﻝ listener. Start();
ﺑﺮﻧﺎﻣﻪ ﺳﺮﻭﺭ using System; using System. Threading; using System. IO; using System. Net. Sockets; using System. Configuration; class Employee. TCPServer{ static Tcp. Listener listener; const int LIMIT = 5; //5 concurrent clients
ﺑﺮﻧﺎﻣﻪ ﺳﺮﻭﺭ public static void Main(){ listener = new Tcp. Listener(2055); listener. Start(); for(int i = 0; i < LIMIT; i++){ Thread t = new Thread(new Thread. Start(Service)); t. Start(); } } public static void Service(){ while(true){ Socket soc = listener. Accept. Socket(); try{ Stream s = new Network. Stream(soc); Stream. Reader sr = new Stream. Reader(s); Stream. Writer sw = new Stream. Writer(s); sw. Auto. Flush = true; // enable automatic flushing sw. Write. Line("{0} Employees available", Configuration. Settings. App. Settings. Count);
ﺑﺮﻧﺎﻣﻪ ﺳﺮﻭﺭ while(true){ string name = sr. Read. Line(); if(name == "" || name == null) break; string job = Configuration. Settings. App. Settings[name]; if(job == null) job = "No such employee"; sw. Write. Line(job); } s. Close(); }catch(Exception e){ } soc. Close(); } } }
ﺑﺮﻧﺎﻣﻪ ﻣﺸﺘﺮی using System; using System. IO; using System. Net. Sockets; class Employee. TCPClient{ public static void Main(string[] args) { Tcp. Client client = new Tcp. Client(args[0], 2055); try{ Stream s = client. Get. Stream(); Stream. Reader sr = new Stream. Reader(s); Stream. Writer sw = new Stream. Writer(s); sw. Auto. Flush = true; Console. Write. Line(sr. Read. Line()); while(true){ Console. Write("Name: "); string name = Console. Read. Line(); sw. Write. Line(name); if(name == "") break; Console. Write. Line(sr. Read. Line()); } s. Close(); }finally{ client. Close(); } } }
: ﺷکﻞ کﻠی ﻣﺘﺪ ﺑﺼﻮﺭﺕ ﺯیﺮ ﺍﺳﺖ Send. To(byte[] data, int Offset, int Size, Socket. Flags, End. Point Remote) Receive. From(byte[] data, ref Endpoint Remote)
SERVER PROGRAM int recv; byte[] data = new byte[1024]; IPEnd. Point ipep = new IPEnd. Point(IPAddress. Any, 9050); Socket newsock = new Socket(Address. Family. Inter. Network, Socket. Type. Dgram, Protocol. Type. Udp); newsock. Bind(ipep); IPEnd. Point sender = new IPEnd. Point(IPAddress. Any, 0); End. Point Remote = (End. Point)(sender); recv = newsock. Receive. From(data, ref Remote); Console. Write. Line("Message received from {0}: ", Remote. To. String()); Console. Write. Line(Encoding. ASCII. Get. String(data, 0, recv)); string welcome = "Welcome to my test server"; data = Encoding. ASCII. Get. Bytes(welcome);
newsock. Send. To(data, data. Length, Socket. Flags. None, Remote); while (true) { data = new byte[1024]; recv = newsock. Receive. From(data, ref Remote); Console. Write. Line(Encoding. ASCII. Get. String(data, 0, recv)); newsock. Send. To(data, recv, Socket. Flags. None, Remote); }
CLIENT PROGRAM byte[] data = new byte[1024]; string input, string. Data; IPEnd. Point ipep = new IPEnd. Point(IPAddress. Parse("127. 0. 0. 1"), 9050); Socket server = new Socket ( Address. Family. Inter. Network, Socket. Type. Dgram, Protocol. Type. Udp); string welcome = "Hello, are you there? "; data = Encoding. ASCII. Get. Bytes(welcome); server. Send. To(data, data. Length, Socket. Flags. None, ipep); IPEnd. Point sender = new IPEnd. Point(IPAddress. Any, 0); End. Point Remote = (End. Point)sender; data = new byte[1024]; int recv = server. Receive. From(data, ref Remote); Console. Write. Line("Message received from {0}: ", Remote. To. String()); Console. Write. Line(Encoding. ASCII. Get. String(data, 0, recv));
while (true) { input = Console. Read. Line(); if (input == "exit") break; server. Send. To(Encoding. ASCII. Get. Bytes(input), Remote); data = new byte[1024]; recv = server. Receive. From(data, ref Remote); string. Data = Encoding. ASCII. Get. String(data, 0, recv); Console. Write. Line(string. Data); } Console. Write. Line("Stopping client"); server. Close();
CLIENT 1 Socket sock = new Socket(Address. Family. Inter. Network, Socket. Type. Dgram, Protocol. Type. Udp); IPEnd. Point iep = new IPEnd. Point(IPAddress. Broadcast, 9050); byte[] data = Encoding. ASCII. Get. Bytes("This is a test message"); sock. Send. To(data, iep); sock. Close();
CLIENT 2 Socket sock = new Socket(Address. Family. Inter. Network, Socket. Type. Dgram, Protocol. Type. Udp); IPEnd. Point iep 1 = new IPEnd. Point(IPAddress. Broadcast, 9050); IPEnd. Point iep 2 = new IPEnd. Point(IPAddress. Parse("192. 168. 0. 255"), 9050); byte[] data = Encoding. ASCII. Get. Bytes("This is a test message"); sock. Set. Socket. Option(Socket. Option. Level. Socket, Socket. Option. Name. Broadcast, 1); sock. Send. To(data, iep 2); sock. Close();
SERVER Socket sock = new Socket(Address. Family. Inter. Network, Socket. Type. Dgram, Protocol. Type. Udp); IPEnd. Point iep = new IPEnd. Point(IPAddress. Any, 9050); sock. Bind(iep); End. Point ep = (End. Point)iep; byte[] data = new byte[1024]; int recv = sock. Receive. From(data, ref ep); string. Data = Encoding. ASCII. Get. String(data, 0, recv); data = new byte[1024]; recv = sock. Receive. From(data, ref ep); string. Data = Encoding. ASCII. Get. String(data, 0, recv); sock. Close();
- Slides: 31