WPF socket communication and wpfsocket Communication
Client:
1 private Socket socket; 2 private IPEndPoint ipEndPoint; 3 private void sendMessageHandler () 4 {5 // server ip, port 192.168.1.1, 8085 6 ipEndPoint = new IPEndPoint (IPAddress. parse ("192.168.1.1"), 8085); 7 socket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); 8 try 9 {10 socket. beginConnect (ipEndPoint, new AsyncCallback (connectHandler), socket); 11} 12 catch (Exception ex) 13 {14 Console. writeLine (ex. message); 15} 16} 17 18 private void connectHandler (IAsyncResult async) 19 {20 Socket client = async. asyncState as Socket; 21 try22 {23 if (client. connected) 24 {25 byte [] msg = Encoding. UTF8.GetBytes ("hello"); 26 try27 {28 System. threading. thread. sleep (1000); 29 socket. send (msg, 0, msg. length, SocketFlags. none); 30} 31 catch (Exception ex) 32 {33 MessageBox. show (ex. toString (); 34} 35} 36 else37 {38 Console. writeLine ("no connection"); 39} 40} 41 catch (Exception ex) 42 {43 Console. writeLine (ex. message); 44} 45}
Server:
1 /// <summary> 2 // data class 3 /// </summary> 4 public class Client 5 {6 public Socket {get; set ;} 7 public byte [] _ buffer; 8 /// <summary> 9 /// buffer 10 for the Socket /// </summary> 11 public byte [] Buffer12 {13 get14 {15 if (_ buffer = null) 16 _ buffer = new byte [10240*10]; 17 return _ buffer; 18} 19} 20} 21 22 private Socket Server; 23 private void Begin () 24 {25 Server = new Socket (AddressFamily. InterNetwork, SocketType. stream, ProtocolType. tcp); 26 Server. bind (new IPEndPoint (IPAddress. any, 8085); 27 Server. listen (100); 28 Server. beginAccept (new AsyncCallback (ClientConnectComplete), null); 29} 30 private int _ receiveCount = 0; // used to count the number of bytes received this time 31 private void ClientConnectComplete (IAsyncResult async) 32 {33 Client client = new Client (); 34 try35 {36 client. socket = Server. endAccept (async); 37} 38 Catch (Exception) 39 {40 Server. BeginAccept (new AsyncCallback (ClientConnectComplete), null); 41 return; 42} 43 Console. WriteLine (client. Socket. RemoteEndPoint + "connection successful! "); 44 45 try46 {47 _ receiveCount = client. socket. receive (client. buffer, 0, client. buffer. length, SocketFlags. none); 48} 49 catch (Exception ex) 50 {51 MessageBox. show (ex. toString (); 52} 53 // received message 54 string message = Encoding. UTF8.GetString (client. buffer, 0, _ receiveCount); 55 // continue to accept the next 56 servers. beginAccept (new AsyncCallback (ClientConnectComplete), null); 57}
C # How does WPF use socket?
It can handle communication, including socket
I am working on a project where the client uses WPF and needs to communicate with the Java Server. What technology should I use on the client?
I am only familiar with wcf. If you want to use wcf, it will be faster and easier to do, And the login is completed by a thread class.
You can create several default silverlight business applications. The above logon is complete. Refer to the above usage.
Or you can write it using socket. In wpf, tcpclient classes are used, and basic socket classes are also provided.