C# Tcp和Socket 网络(五)

TcpReceive

 1 public Form1()
 2         {
 3             InitializeComponent();
 4             new Thread(() =>
 5             {
 6                 IPAddress ip = IPAddress.Parse(ip地址);
 7                 Int32 port = 13745;
 8                 TcpListener listen = new TcpListener(ip, port);
 9                 listen.Start();
10                 TcpClient tc = listen.AcceptTcpClient();
11 
12                 NetworkStream ns = tc.GetStream();
13                 StreamReader sr = new StreamReader(ns);
14                 string result = sr.ReadToEnd();
15 
16                 Invoke(new MethodInvoker(delegate() { textBox1.Text = result; }));
17                 sr.Close();
18                 ns.Close();
19                 tc .Close();
20                 listen.Stop();
21 
22 
23             }).Start();
24         }

 

TcpSend

 1  public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             TcpClient client = new TcpClient(ip地址, Int32.Parse(端口号));
11             NetworkStream ns = client.GetStream();
12             FileStream fs = File.Open("Form1.cs", FileMode.Open);
13             int data = fs.ReadByte();
14             while(data!=-1)
15             {
16                 ns.WriteByte((byte)data);
17                 data = fs.ReadByte();
18             }
19             fs.Close();
20             ns.Close();
21             client.Close();
22         }
23     }

Socket Client

 1 static void Main(string[] args)
 2         {
 3 
 4             IPHostEntry ipHsot = Dns.Resolve(ip地址);
 5             IPAddress ipAddress = ipHsot.AddressList[0];
 6             IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 13745);
 7             Console.WriteLine("Starting : Creating Socket object");
 8             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 9             s.Connect(ipEndPoint);
10             Console.WriteLine("Successfully Connected to {0}", s.RemoteEndPoint);
11             while (true)
12             {
13                 byte[] receivedBytes = new byte[1024];
14                 string sendMessage = Console.ReadLine();
15                 if (sendMessage.Contains("exit"))
16                     break;
17                 Console.WriteLine("Creating message : {0}", sendMessage);
18                 byte[] forwardMessage = new UTF8Encoding().GetBytes(sendMessage + "[FINAL]");
19 
20                 if (s.Connected)
21                 {
22                     try
23                     {
24                         s.Send(forwardMessage);
25                         int totalBytesReceived = s.Receive(receivedBytes);
26                         Console.WriteLine("Message provided from server :{0}", new UTF8Encoding().GetString(receivedBytes, 0, totalBytesReceived));
27                     }
28                     catch (Exception ex)
29                     {
30                         throw ex;
31                     }
32                 }
33             }
34             s.Shutdown(SocketShutdown.Both);
35             s.Close();
36 
37         }

 

Socket Server

 1  static void Main(string[] args)
 2         {
 3             Console.WriteLine("Starting : Createing Socket object");
 4             Socket lisner = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 5             lisner.Bind(new IPEndPoint(IPAddress.Any, 13745));
 6             lisner.Listen(10);
 7             Console.WriteLine("Waiting for connection on port 13745");
 8             Socket socket = lisner.Accept();
 9             while (true)
10             {  
11                 string receivedValue = string.Empty;
12                 while (true)
13                 {
14                     byte[] receivedBytes = new byte[1024];
15                     int numBytes = socket.Receive(receivedBytes);
16                     Console.WriteLine("Receiving / ");
17                     receivedValue += new UTF8Encoding().GetString(receivedBytes, 0, numBytes);
18                     if (receivedValue.IndexOf("[FINAL]") > -1)
19                         break;
20                 }
21                 Console.WriteLine("Received value: {0}", receivedValue);
22 
23                 string replyValue = Console.ReadLine();
24                 byte[] replyMessage = new UTF8Encoding().GetBytes(replyValue);
25                 socket.Send(replyMessage);
26                // socket.Shutdown(SocketShutdown.Both);
27                 //socket.Close();
28             }
29            
30         }
31     }

 

posted @ 2016-11-30 15:43  指间的徘徊  阅读(323)  评论(0编辑  收藏  举报