zoukankan      html  css  js  c++  java
  • 基于TCP通讯的简单示例

    定义好通讯指令:

    GET X [Name]

    X可以是0和1,如果X是1的话那么括号里面的Name不得为空,这三项用空格分开

    首先定义一个数据集合:

    public class Services 
    {
      public string[] Name; 
      public int[] Score;
    
      public Services() 
      { 
        Name = new[] { "star", "hewie", "fanfan", "peak", "junyi" }; 
        Score = new[] { 100, 80, 60, 40, 20 }; 
      } 
    }

    服务端实现:侦听60000端口信息

    namespace StudentScoreServer 
    { 
    static class IExt 
    { 
    public static byte[] ByteData(this string s) 
    { 
    return System.Text.Encoding.Default.GetBytes(s); 
    } 
    }
    
    class Program 
    { 
    static void Main(string[] args) 
    { 
    Console.WriteLine("initializing server..."); 
    int port = 60000;//端口侦听
    
    System.Net.Sockets.TcpListener listener; 
    try 
    { 
    listener = new System.Net.Sockets.TcpListener(port); 
    } 
    catch 
    { 
    Console.WriteLine("port failed..."); 
    return; 
    }
    
    listener.Start();
    
    Console.WriteLine("start listener");
    
    Console.WriteLine("server initialized waiting for incoming connections..."); 
    bool loop = true;
    
    string errorCommand = "the command is error..."; 
    while (loop) 
    { 
    Socket socket = listener.AcceptSocket(); 
    NetworkStream stream = new NetworkStream(socket); 
    StreamReader reader = new StreamReader(stream); 
    string command = reader.ReadLine(); 
    string[] rec = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    
    try 
    { 
    if (rec[0] != "GET") 
    { 
    throw new Exception(); 
    }
    
    Services services = new Services(); 
    if (rec[1] == "0") 
    { 
    for (int i = 0; i < services.Name.Length; i++) 
    { 
    string tmp = services.Name[i] + " ..." + services.Score[i] + "...
    "; 
    socket.Send(tmp.ByteData()); 
    } 
    } 
    else if (rec[1] == "1") 
    { 
    string key = rec[2]; 
    int i; 
    for (i = 0; i < services.Name.Length; i++) 
    { 
    if (services.Name[i].Equals(key)) 
    { 
    string tmp = services.Name[i] + " ..." + services.Score[i] + "...
    "; 
    socket.Send(tmp.ByteData()); 
    break; 
    } 
    }
    
    if (i == services.Name.GetLength(0)) 
    { 
    string tmp = "the name is not find ...
    "; 
    socket.Send(tmp.ByteData()); 
    } 
    } 
    } 
    catch 
    { 
    socket.Send(errorCommand.ByteData()); 
    }
    
    string end = "
    "; 
    socket.Send(end.ByteData()); 
    } 
    } 
    } 
    }

    客户端实现:

    namespace StudentScoreClient 
    { 
    static class IExt 
    { 
    public static byte[] ByteData(this string s) 
    { 
    return System.Text.Encoding.ASCII.GetBytes(s.ToCharArray()); 
    }
    
    } 
    class Program 
    { 
    private static TcpClient client = null; 
    private static NetworkStream outStream = null; 
    private static StreamReader inStream = null;
    
    static void Main(string[] args) 
    { 
    Console.WriteLine("client start connection...."); 
    
    string serverIP = "127.0.0.1"; 
    int port = 60000;
    
    try 
    { 
    client = new TcpClient(serverIP,port); 
    outStream = client.GetStream(); 
    inStream = new StreamReader(outStream);
    
    } 
    catch 
    { 
    Console.WriteLine("不能连接到服务端!"); 
    return; 
    }
    
    Console.WriteLine("please input some command....such as GET 0 or GET 1 star"); 
    string command = Console.ReadLine()+"
    "; 
    while (true) 
    { 
    if (string.IsNullOrEmpty(command)) 
    { 
    break; 
    }
    
    outStream.Write(command.ByteData(),0,command.ByteData().Length);//发送请求指令
    
    while (true) 
    { 
    string rec = inStream.ReadLine(); 
    if (string.IsNullOrEmpty(rec)) 
    { 
    break; 
    } 
    Console.WriteLine(rec); 
    }
    
    Console.WriteLine("please input some command again"); 
    command = Console.ReadLine()+"
    "; 
    }
    
    client.Close(); 
    Console.WriteLine("press return to exit"); 
    Console.ReadLine(); 
    } 
    } 
    }

    于17:16 2013/10/16编写,但有些代码还不是很懂。

  • 相关阅读:
    C#微信开发
    3-4:字符串方法
    2-4-1 元组
    2-3-3 列表方法
    2-2-3:序列(字符串)乘法(p32)
    3-3字符串格式化(p47)
    2-2:分片
    2-1:Print date(p28)
    old.2.三次登录机会
    old.2.sum(1-2+3-4+...+99)
  • 原文地址:https://www.cnblogs.com/hackztx/p/3372597.html
Copyright © 2011-2022 走看看