用C#怎么实现客户端和服务器的交互

用C#怎么实现客户端和服务器的交互,第1张

ASPNET(C#)提供的常用内置对象有
1 Request: 接收客户端信息
2 Response: 传送信息给客户端。
至于具体的用法,你可在网上百度哈。

session的工作原理如下,即为sessionid在客户端和浏览器端的交互:
当浏览器A去访问一个有打开了session机制的php1文件时,session文件会创建,同时会将该session文件的id以cookie的形式
发送给浏览器(session文件的id号可以session_id()这个函数来获得)。当浏览器A没有关闭(会话没有结束),反而去访问另一个也开启
session机制的php2文件时,那么该sessionid也会跟着>帖个以前做过的简单代码,写的不好,不过大概原理应该是这样,希望有用。
服务器代码
public partial class ServerMain : Form
{
private IPEndPoint ServerInfo;//存放服务器的IP和端口信息
private Socket ServerSocket;//服务端运行的SOCKET
//private Socket ChildSocket=null;
private Thread ServerThread;//服务端运行的线程
private Socket[] ClientSocket;//为客户端建立的SOCKET连接
private int ClientNumb;//存放客户端数量
private byte[] MsgBuffer;//接受客户发的信息(字节)
string msg;//接受客户发的信息(字符串)
SqlSearch search=new SqlSearch();
private byte[] Mybuffer;
byte[] MYuserName=null;//保存从数据库获取的客户名单(字节)
public ServerMain()
{
InitializeComponent();
}
private void ServerMain_Load(object sender, EventArgs e)
{
thisCmdStartEnabled = true;
thisCmdStopEnabled = false;
}
private void CmdStart_Click(object sender, EventArgs e)
{
ServerSocket = new Socket(AddressFamilyInterNetwork, SocketTypeStream, ProtocolTypeTcp);
ServerInfo=new IPEndPoint(IPAddressAny,6666);//thisGetPort());
ServerSocketBind(ServerInfo);//将SOCKET接口和IP端口绑定
ServerSocketListen(10);//开始监听,并且挂起数为10

ClientSocket = new Socket[1000];//为客户端提供连接个数
MsgBuffer = new byte[50005000];//消息数据大小
ClientNumb = 0;//数量从0开始统计

ServerThread = new Thread(RecieveAccept);//将接受客户端连接的方法委托给线程
ServerThreadIsBackground = true;
ServerThreadStart();//线程开始运行

//CheckForIllegalCrossThreadCalls = false;//不捕获对错误线程的调用

thisCmdStartEnabled = false;
thisCmdStopEnabled = true;
thisStateMsgText = "服务正在运行" + " 运行端口:" + "6666";// thisGetPort()ToString();
thisClientListItemsAdd("服务于 " + DateTimeNowToString() + " 开始运行");
}
private void RecieveAccept()
{
while (true)
{
ClientSocket[ClientNumb] = ServerSocketAccept();

ClientSocket[ClientNumb]BeginReceive(MsgBuffer, 0, MsgBufferLength, 0, new AsyncCallback(RecieveCallBack), ClientSocket[ClientNumb]);
thisClientListItemsAdd(ClientSocket[ClientNumb]RemoteEndPointToString() + " 成功连接服务器");
ClientNumb++;
}
}
private void RecieveCallBack(IAsyncResult AR)
{
try
{
Socket RSocket = (Socket)ARAsyncState;
int REnd = RSocketEndReceive(AR);
for (int i = 0; i < ClientNumb; i++)
{
if (ClientSocket[i]Connected)
{
//ClientSocket[i]Send(MsgBuffer, 0, REnd, 0);

msg = EncodingUnicodeGetString(MsgBuffer, 0, REnd); //1024 200 + ')
msg = msgSubstring(0,4);
if (msg == "name")//注册判断
if (msg == "User")//判断发送的信息
{
msg = EncodingUnicodeGetString(MsgBuffer,0,REnd);
string[] recMsg= msgSplit('|');

string Myname = recMsg[1];
string[] name = searchName();
string friendString = "";
for (int k = 0; k < nameLength; k++)
{
friendString += name[k] + "|";//把好友放在Friend中
}
string[] EachName=friendStringSplit('|');
for (int j = 0; j < EachNameLength; j++)
{

if (MynameEquals(EachName[j]))
{

Mybuffer = EncodingUnicodeGetBytes(msg);
}
}

ClientSocket[i]Send(Mybuffer, 0, MybufferLength, SocketFlagsNone);
}
}
RSocketBeginReceive(MsgBuffer, 0, MsgBufferLength, 0, new AsyncCallback(RecieveCallBack), RSocket);
}
}
catch { }
}
private void CmdStop_Click(object sender, EventArgs e)
{
ServerThreadAbort();//线程终止
ServerSocketClose();//关闭SOCKET

thisCmdStartEnabled = true;
thisCmdStopEnabled = false;
thisStateMsgText = "等待运行";
thisClientListItemsAdd("服务于 " + DateTimeNowToString() + " 停止运行");
}
private void timer1_Tick(object sender, EventArgs e)
{
label1Text = "当前时间为: "+DateTimeNowToString();
label2Text = "在线人数: " + ClientNumb;
}
}
}
客户端:
public partial class Online : Form
{
private IPEndPoint ServerInfo;
private Socket ClientSocket;
Friend friend = new Friend();
string id, password,msg;
byte[] sendinfo;
byte[] buffer=new byte[1024];

public Online()
{
InitializeComponent();
}
private void btnEnter_Click(object sender, EventArgs e)
{
ServerInfo = new IPEndPoint(IPAddressParse("1921681121"), 6666);
try
{

ClientSocketConnect(ServerInfo);
//MessageBoxShow("登录服务器成功!\n");
id = textIDText;
password = TextPassWordText;
msg = "info"+" "+ id+" " + password;//加消息头info
sendinfo = EncodingUnicodeGetBytes(msg);
ClientSocketSend(sendinfo);//获取登录信息并发给服务器
ClientSocketBeginReceive(buffer, 0, bufferLength, 0, new AsyncCallback(ReceiveCallBack), null);//接受服务器发来的登录信息

//thisbtnEnterEnabled = false;
}
catch
{
MessageBoxShow("登录服务器失败,请确认服务器是否正常工作!");
}
}
private void ReceiveCallBack(IAsyncResult AR)
{
try
{
int REnd = ClientSocketEndReceive(AR);
string recMsg = EncodingUnicodeGetString(buffer, 0, REnd);
string cmd = recMsgSubstring(0, 2);
if (cmd == "ok")//根据服务器发来信息判断是否登录成功
{
thisVisible = false;
friendShowDialog();
}
if(cmd=="fl")
{
MessageBoxShow("密码有误!登录失败!");
}
ClientSocketBeginReceive(buffer, 0, bufferLength, 0, new AsyncCallback(ReceiveCallBack), null);
}
catch
{
MessageBoxShow("!");
}
}
private void register_Load(object sender, EventArgs e)
{
ClientSocket = new Socket(AddressFamilyInterNetwork, SocketTypeStream, ProtocolTypeTcp);
CheckForIllegalCrossThreadCalls = false;

}
private void linkRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Register reg = new Register();
regShow();
}

把服务器看成是文件中转站就行了,从客户端接收消息,再转发到目标客户端。
source
<->
server
<=>
destination


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/13319506.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-14
下一篇 2023-07-14

发表评论

登录后才能评论

评论列表(0条)

保存