C#写的Socket Server 和Client

tony_jin 2004-11-26 01:07:42
该如何写啊?最好有现成的源代码使用,时间太急
...全文
751 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
magic2003 2004-12-21
  • 打赏
  • 举报
回复
wangjun110 2004-12-05
  • 打赏
  • 举报
回复
好好学习...
lucbesson 2004-12-03
  • 打赏
  • 举报
回复
http://dev.csdn.net/develop/article/26/26707.shtm 服务器端

http://dev.csdn.net/develop/article/26/26708.shtm 客户端
flyinpiggy 2004-11-27
  • 打赏
  • 举报
回复
老是提示我太长了
请分开回复
楼主要的话给我邮箱
我发给你
adailee 2004-11-27
  • 打赏
  • 举报
回复
悠着点,慢慢学。
xiaoxiao5020 2004-11-26
  • 打赏
  • 举报
回复
看着眼运了,真牛!
sidshen 2004-11-26
  • 打赏
  • 举报
回复
以下是我刚开始用socket时写的一个服务器端,希望有点儿用,当初是是用来接收sms上行用的

using System;
using System.Net.Sockets;
using System.Text;

public class TcpTimeServer
{
private const int portNum = 9901;
public static int Main(String[] args)
{
bool done = false;
TcpListener listener = new TcpListener(portNum);
listener.Start();
Console.WriteLine("....................");
Console.WriteLine("....开始监听端口....");
Console.WriteLine("....................");
while (!done)
{
Console.Write("Waiting for connection...");
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Connection accepted.");
NetworkStream ns = client.GetStream();
try
{
byte[] bytes = new byte[1024];
int bytesRead = ns.Read(bytes, 0, bytes.Length);
Console.WriteLine(".......................接受到的信息..........................");
string MOMsg=Encoding.Default.GetString(bytes,0,bytesRead);
Console.WriteLine(MOMsg);
// MO objMO=new MO(MOMsg);
// Console.WriteLine("---------");
// Console.WriteLine("Msg_type="+objMO.Msg_type);
// Console.WriteLine("GWName="+objMO.GWName);
// Console.WriteLine("GWPassword="+objMO.GWPassword);
// Console.WriteLine("Srctermid="+objMO.Srctermid);
// Console.WriteLine("Dsttermid="+objMO.Dsttermid);
// Console.WriteLine("Msg="+objMO.Msg);
Console.WriteLine(".......................发送的返回正确的信息信息..........................");
//正确处理返回1
string RspMSG="1";
Console.WriteLine(RspMSG);
bytes=Encoding.Default.GetBytes(RspMSG);
ns.Write(bytes, 0, bytes.Length);

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.WriteLine(".......................发送的返回错误的信息信息..........................");
//错误处理返回0
string RspMSG="01";
byte[] bytes =Encoding.Default.GetBytes(RspMSG);
Console.WriteLine(RspMSG);
ns.Write(bytes, 0, bytes.Length);
}
finally
{
ns.Close();
client.Close();
}
}
listener.Stop();
return 0;
}

}
class MO
{
/// <summary>
/// 0状态报告 1 iod点播 最长1位
/// </summary>
public readonly string Msg_type;
/// <summary>
/// 连接名 最长10 位
/// </summary>
public readonly string GWName;
/// <summary>
/// 连接密码 最长10 位
/// </summary>
public readonly string GWPassword;
/// <summary>
/// 目的号码21位
/// </summary>
public readonly string Dsttermid;
/// <summary>
/// 源号码(上行的手机号码)
/// </summary>
public readonly string Srctermid;
/// <summary>
/// 消息体 150位
/// </summary>
public readonly string Msg;
public MO(string MOMsg)
{
Msg_type=MOMsg.Substring(0,1).Trim();
GWName=MOMsg.Substring(1,10).Trim();
GWPassword=MOMsg.Substring(11,10).Trim();
Dsttermid=MOMsg.Substring(21,21).Trim();
Srctermid=MOMsg.Substring(42,21).Trim();
Msg=MOMsg.Substring(63).Trim();
}
}
huqiyang 2004-11-26
  • 打赏
  • 举报
回复
bs
coffeenight 2004-11-26
  • 打赏
  • 举报
回复
only these code?
Lastcsdner 2004-11-26
  • 打赏
  • 举报
回复
懒,当初我写这个东西什么都不知道也就花半天就写好了。
给你点建议吧:客户端使用TCPClient,如果需要操作裸SOCKET就继承一下;
服务端给你一点代码吧:
IPAddress localAddr = IPAddress.Parse("0.0.0.0");
TcpListener server = new TcpListener(localAddr,_port);
server.Start() ;

while(running)
{
if(server.Pending())
{
_sockQueue.Enqueue(new SocketHandle(server.AcceptSocket())) ;
}
else
{
Thread.Sleep(10) ;
}
}
server.Stop() ;

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧