- 1
- 0
- 约1.4万字
- 约 13页
- 2017-01-28 发布于重庆
- 举报
flex与c#基于socket的实时互动网络游戏编程教程
近些年webGame非常火爆,可惜相关教程实在少之又少,在我学习过程中无数次baidu,google。发现实际涉及wenGame核心的东西基本没有。于是就有了把我学习过程中使用和总结的代码拿上来给大家分享,让有共同爱好的同学们少走弯路。
本教程基于flex与c#,做到完全同步的游戏设计与编写。本教程只提供实现基本功能的代码,只要融会贯通,就能在此基础上制作出无比强大的网络游戏。
这篇教程最好是对c#有一定基础,会使用vs的同学。如果不会,我推荐看一下《c#网络应用编程》。
先来段c#下基由socket连接的代码,什么不是讲flex?flex连接服务器端也是用c#写的(其实也可以用c++或者java写,不过原理都是一样,一通百通)
说明,c#封装了socket,提供操作更方便的Tcpclient与TcpListener,c#属于
服务器端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace TestOnlyServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// summary使用的本机IP地址/summary
IPAddress localAddress;
/// summary监听端口/summary
private const int port = 51888;
private TcpListener myListener;
public BinaryReader br;//读取连接用户发来的数据
private BinaryWriter bw;//给连接用户发送数据
private void Form1_Load(object sender, EventArgs e)
{
// IPAddress[] addrIP = Dns.GetHostAddresses(Dns.GetHostName());
// localAddress = addrIP[0];
localAddress = IPAddress.Parse(00);//换成本机ip
}
private void button1_Click(object sender, EventArgs e)
{//在设计面版加一个按钮并添加一个事件
myListener = new TcpListener(localAddress, port); //检听端口设置
myListener.Start();//开始检听端口
textBox1.Text = string.Format(开始在{0}:{1}监听客户连接, localAddress, port);
//创建一个线程监听客户端连接请求
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();//开始线程,其实线程的概念很好理解,就是执行一个函数,不过这个函数只和其他函数是并发的,并且占用的服务器资源比较小,因为只用了一部分计算资源
}
/// summary接收客户端连接/summary
///
private void ListenClientConnect()
{
TcpClient newClient = null;
while (true)
原创力文档

文档评论(0)