Mình có tham khảo cái quyền sách trong topic này:
http://forums.congdongcviet.com/showthread.php?t=25639
Mình có làm 1 cái ví dụ trong đó. Khi chạy cả 2 file cùng lúc ở máy của mình thì nó hoạt động tốt.
Còn mang sang 2 máy khác nhau. (Nhưng cùng dùng chung mạng quán nét)
Thì không thấy nó chạy j cả. Mà cũng không thấy báo lỗi. Chỉ thấy win báo gửi lỗi cho họ:
Cả 2 máy mình đều dùng winXP, cài đủ .NET....
Code của 2 file Sever và Client:
Server:
Mã:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets; namespace Code_Server{ class Program { static void Main(string[] args) { IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2008); Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); server.Bind(iep); server.Listen(10); Console.WriteLine("Cho ket noi tu Client"); Socket client = server.Accept(); Console.WriteLine("Chap nhan ket noi tu:{0}", client.RemoteEndPoint.ToString()); string s = "Chao ban den noi sever"; //chuyen chuoi s thanh dang byte byte[] data = new byte[1024]; data = Encoding.ASCII.GetBytes(s); //Gui nhan du lieu theo phuong thuc da thiet ke client.Send(data, data.Length, SocketFlags.None); while (true) { data = new byte[1024]; int recv = client.Receive(data); if (recv == 0) break; //chuyen mang byte Data thanh chuoi va in ra man hinh s = Encoding.ASCII.GetString(data, 0, recv); //Console.WriteLine("Client gui len:{0}", recv); Console.WriteLine("Client:{0}", s); //Neu nhan duoc chuoi Quit thi thoat if (s.ToUpper().Equals("QUIT")) break; //Gui tra lai cho client chuoi s //s = s.ToUpper(); s = Console.ReadLine(); data = new byte[1024]; data = Encoding.ASCII.GetBytes(s); client.Send(data, data.Length, SocketFlags.None); } client.Shutdown(SocketShutdown.Both); client.Close(); server.Close(); } }}
Code Client:
Mã:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets; namespace Code_Client{ class Program { static void Main(string[] args) { IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2008); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(iep); byte[] data = new byte[1024]; int recv = client.Receive(data); string s = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine("Server gui:{0}",s); string input; while (true) { input = Console.ReadLine(); //chuyen input thanh mang byte gui len sever data = new byte[1024]; data = Encoding.ASCII.GetBytes(input); client.Send(data, data.Length, SocketFlags.None); if (input.ToUpper().Equals("QUIT")) break; data = new byte[1024]; recv = client.Receive(data); s = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine("Sever gui:{0}", s); } client.Disconnect(true); client.Close(); } }}
Mong mọi người góp ý hộ.
Mục đích của 2 code là chạy trên 2 máy tính trong mạng Lan mà nếu thế này thì ...[IMG]images/smilies/17.gif[/IMG]
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ