iOS app 和服务端怎么交互,实现注册登录功能

iOS app 和服务端怎么交互,实现注册登录功能,第1张

IOS开发终端和服务器交互目前都是通过restful api的形式。
IOS终端列出需要的API接口,服务端实现这些接口,中间通过>

用twisted,用工厂管理连接,每个连接建立transport使用简单方便!参看下面代码:

#Copyright(c)ThePyAMFProject

#SeeLICENSEtxtfordetails

\"\"\"

ExamplesocketserverusingTwisted

@see:U{Documentationforthisexample

raiseSystemExit

fromtwistedinternetprotocolimportProtocol,Factory

fromtwistedinternetimportreactor

fromdatetimeimportdatetime

importpyamf

classTimerProtocol(Protocol):

interval=10#客户端链接到server后,server往客户端发送时间的间隔

encoding=pyamfAMF3

timeout=20#客户端链接到server后多少时间不 *** 作就断开链接的timeout

def__init__(self):

selfstarted=False

#设置编码器

selfencoder=pyamfget_encoder(selfencoding)、

#设置server端将数据编码成amf后存放的缓存地址

selfstream=selfencoderstream

defconnectionLost(self,reason):

ProtocolconnectionLost(self,reason)

print\"locstconnection:

帖个以前做过的简单代码,写的不好,不过大概原理应该是这样,希望有用。
服务器代码
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();
}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存