using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; //添加引用 using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace SocketServer { public partial class Form1 : Form { Thread mythread;//创建线程 Socket socket; int port = 8000;//定义侦听端口号
while(!isstopped)
{
TcpClient client = tcpl.AcceptTcpClient();
}
2,这样只是接受了多客户端响应,还需要为每一个客户端分配一个线程,让他在线程中完成与客户端交互
Thread user = new Thread(ClientStart);
user.Start(client);
3.读取客户端数据可以使用NetworkStream 的read方法,发送客户端信息可以使用NetworkStream 的write方法
4.注意下,socket通讯需要自己定义好协议,芯跳协议也需要考虑和实现
多读别人代码学习是个捷径,网上开源的socket服务器代码一大堆
现在还要找下flash安全策略方面的东西,写一个了安全策略文件但是还是无法和服务端交互..
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
//添加引用
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SocketServer
{
public partial class Form1 : Form
{
Thread mythread;//创建线程
Socket socket;
int port = 8000;//定义侦听端口号
public Form1()//服务器端
{
InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
{
//是跨线程操作
Control.CheckForIllegalCrossThreadCalls = false;
} //获取本机IP地址
public static IPAddress GetServerIP()
{
IPHostEntry ieh = Dns.GetHostEntry(Dns.GetHostName());
return ieh.AddressList[0];
}
//监听
private void BeginListen()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ServerIp = GetServerIP();
IPEndPoint iep = new IPEndPoint(ServerIp, port);
socket.Bind(iep); toolStripStatusLabel1.Text = iep.ToString() + "正在监听...";
byte[] byteMessage = new byte[100];
while (true)
{
try
{
socket.Listen(5);
Socket newSocket = socket.Accept();
newSocket.Receive(byteMessage); string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + "-" + "信息来自:";
msg += newSocket.RemoteEndPoint.ToString() +" "+ Encoding.Default.GetString(byteMessage).Trim(new char[] {'\0'});
this.richTextBox1.AppendText(msg + "\r\n");
}
catch (SocketException ex)
{
toolStripStatusLabel1.Text += ex.ToString();
}
}
}
private void btn_Listen_Click(object sender, EventArgs e)
{
try
{
mythread = new Thread(new ThreadStart(BeginListen));
mythread.Start();
}
catch (System.Exception er)
{
MessageBox.Show(er.Message, "完成", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}
}////////////////////////////////////////////////////////////////////////////////////
//客户端
using System;
using System.Windows.Forms;
//添加引用
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace SocketClient
{
public partial class Form1 : Form
{
public Form1()//客户端
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//得到本机地址
IPHostEntry ieh = Dns.GetHostEntry(Dns.GetHostName());
txtIP.Text = ieh.AddressList[0].ToString();
}
private void btn_Send_Click(object sender, EventArgs e)
{
BeginSend();
}
//发送信息
private void BeginSend()
{
string ip = txtIP.Text;
string port = txtPort.Text;
string msg = txtMsg.Text.Trim();
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress serverIp = IPAddress.Parse(ip);
int serverPort = Convert.ToInt32(port);
IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
socket.Connect(iep); byte[] byteMessage;
byteMessage = Encoding.ASCII.GetBytes(msg);
socket.Send(byteMessage);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
}