社区
C#
帖子详情
C#写的Socket Server 和Client
tony_jin
2004-11-26 01:07:42
该如何写啊?最好有现成的源代码使用,时间太急
...全文
808
10
打赏
收藏
C#写的Socket Server 和Client
该如何写啊?最好有现成的源代码使用,时间太急
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
用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() ;
C#
基于
Socket
的TCP通讯例程
Server
Client
一、简单了解服务端和客户端各自的功能 首先应该清楚服务端(
Server
)和客户端(
Client
)它们各自的功能。 (1)服务端(
Server
):负责接收客户端的请求,然后根据客户端请求的内容不同而给客户端返回相应的数据。 (2)客户端(
Client
):接服务端,向服务端发送自己的业务需求(也就是数据),然后接受服务端返回过来的信息。 (3)分析服务端和客户端的功能,可以很清楚的知道,它们完成了数据之间的交流,或者说是业务之间的相互传递与获取。 二、服务器与客户端之间信息传递的桥梁(
Socket
) (1)服务
Socket
实现TCP通信(
C#
实现)
Server
和
Client
是一对好朋友,有一天
Client
想知道知道现在几点了就给
Server
打电话(emmmm就是想他了的意思)
Client
把他的手机卡
client
fd插到手机卡槽里,拨出了
Server
的手机号(ip+port): //连接
Socket
client
fd = new
Socket
(AddressFamily.InterNetwork,
Socket
Type.Stream...
C#
Socket
通信
Server
-
client
chatContent.AppendText(“客户端(” + GetCurrentTime() + “):” + msg + “\r\n”);chatContent.AppendText(“客户端(” + GetCurrentTime() + “):” + msg + “\r\n”);ip:”+ip+“,端口:”+port.Text.Trim()+“\r\n”);chatContent.AppendText(“服务端(”+GetCurrentTime()+“):”+msg+“\r\n”);
c#
socket
(
server
and
client
)
server
代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.N
delphi 如何判断
socket
连接成功_【
C#
Socket
】如何检测
Client
已经断开连接 ?如何正确地断开
Socket
连接 ?...
这个是问题描述。知乎问题描述不能超过 3000 字,所以放这里。问题连结:【
C#
Socket
】如何检测
Client
已经断开连接 ?如何正确地断开
Socket
连接 ? - 知乎 先附上
Server
的完整代码:using System; using System.Net; using System.Net.
Socket
s; using System.Text; using System....
C#
111,128
社区成员
642,533
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章