WPF uses sockets for simple chat software

Source: Internet
Author: User
Tags clear screen

Company network restrictions can not transfer files, first paste part of the code

Project structure:

1. Solution

1.1. Client

1.2. Server

Client:

<Windowx:class= "Csharpsocketexample.mainwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= "375"Width= "524">    <GridMargin= "0,0,2,-20">        <ButtonName= "Btn_connection"Content= "Send"HorizontalAlignment= "Left"Margin= "292,288,0,0"VerticalAlignment= "Top"Width= " the"Click= "Btn_connection_click"/>        <TextBlockName= "Tblock_message"HorizontalAlignment= "Left"Margin= "34,10,0,0"textwrapping= "Wrap"VerticalAlignment= "Top"Height= "208"Width= "464"/>        <ButtonName= "Btn_refresh"Content= "Refresh"HorizontalAlignment= "Left"Margin= "292,244,0,0"VerticalAlignment= "Top"Width= " the"Click= "Btn_refresh_click"/>        <LabelContent= "nickname"HorizontalAlignment= "Left"Margin= "34,244,0,0"VerticalAlignment= "Top"/>        <TextBoxx:name= "Tb_username"HorizontalAlignment= "Left"Height= "All"Margin= "92,247,0,0"textwrapping= "Wrap"VerticalAlignment= "Top"Width= "136"/>        <LabelContent= "message"HorizontalAlignment= "Left"Margin= "34,284,0,0"VerticalAlignment= "Top"/>        <ButtonName= "Btn_clear"Content= "Clear Screen"HorizontalAlignment= "Left"Margin= "401,244,0,0"VerticalAlignment= "Top"Width= " the"Click= "Btn_clear_click"/>

Private voidBtn_connection_click (Objectsender, RoutedEventArgs e) {            intPort =6000; stringHost ="127.0.0.1"; IPAddress IP=Ipaddress.parse (host); IPEndPoint Ipe=Newipendpoint (IP, port); Socket Clientsocket=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Clientsocket.connect (IPE);//Send Message            stringSendstr =tb_username. Text +": "+Tb_message.            Text; byte[] Sendbytes =Encoding.UTF8.GetBytes (SENDSTR);            Clientsocket.send (sendbytes); //Receive Message            stringRecstr =""; byte[] Recbytes =New byte[4096]; intbytes = Clientsocket.receive (recbytes, Recbytes.length,0); Recstr+ = Encoding.UTF8.GetString (Recbytes,0, bytes); Tblock_message. Text=Recstr;clientsocket.close (); }        Private voidBtn_refresh_click (Objectsender, RoutedEventArgs e) {            intPort =6000; stringHost ="127.0.0.1"; IPAddress IP =Ipaddress.parse (host); IPEndPoint Ipe=Newipendpoint (IP, port); Socket Clientsocket=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);            Clientsocket.connect (IPE); //Send Message            stringSendstr ="Refresh"; byte[] Sendbytes =Encoding.UTF8.GetBytes (SENDSTR); Clientsocket.send (sendbytes);//Receive Message            stringRecstr =""; byte[] Recbytes =New byte[4096]; intbytes = Clientsocket.receive (recbytes, Recbytes.length,0); Recstr+ = Encoding.UTF8.GetString (Recbytes,0, bytes); Tblock_message. Text=Recstr;        Clientsocket.close (); }

Server:

namespaceserver{classProgram {Static voidMain (string[] args) {            intPort =6000; stringHost ="127.0.0.1"; IPAddress IP=Ipaddress.parse (host); IPEndPoint Ipe=Newipendpoint (IP, port); Socket Socket=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket.            Bind (IPE); Socket. Listen (0); Console.WriteLine ("Listen Server Open."); stringHistory ="";  while(true)            {                //Receive MessageSocket ServerSocket =socket.                Accept (); Console.WriteLine ("Connection successful."); history+="\ r"; stringHistory ="";  while(true)            {                //Receive MessageSocket ServerSocket =socket.                Accept (); Console.WriteLine ("Connection successful."); history+="\ r"; stringRecstr =""; byte[] RecByte =New byte[4096]; intbytes = Serversocket.receive (RecByte, Recbyte.length,0); Recstr+ = Encoding.UTF8.GetString (RecByte,0, bytes); if(Recstr! ="Refresh") { history+=Recstr; }                //Send MessageConsole.WriteLine ("Receive message:{0}", RECSTR); stringSendstr =History .                ToString (); byte[] Sendbyte =Encoding.UTF8.GetBytes (SENDSTR); Serversocket.send (Sendbyte, Sendbyte.length,0);                Serversocket.close (); //socket. Close ();

WPF uses sockets for simple chat software

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.