基于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] + "...\r\n"; 
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] + "...\r\n"; 
socket.Send(tmp.ByteData()); 
break; 
} 
}

if (i == services.Name.GetLength(0)) 
{ 
string tmp = "the name is not find ...\r\n"; 
socket.Send(tmp.ByteData()); 
} 
} 
} 
catch 
{ 
socket.Send(errorCommand.ByteData()); 
}

string end = "\r\n"; 
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()+"\r\n"; 
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()+"\r\n"; 
}

client.Close(); 
Console.WriteLine("press return to exit"); 
Console.ReadLine(); 
} 
} 
}

 

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

posted on 2013-10-16 17:14  张天星  阅读(153)  评论(0编辑  收藏  举报

导航