//发送登陆成功的消息,不发送登陆者
if (clientInfologin.socket != clientSocket)
{
//Send the message to all users
clientInfologin.socket.BeginSend(message, 0, message.Length, SocketFlags.None,
new AsyncCallback(OnSend), clientInfologin.socket);
}
}
break;
case Command.Logout:
//When a user wants to log out of the server then we search for her
//in the list of clients and close the corresponding connection
int nIndex = 0;
foreach (ClientInfo client in clientList)
{
if (client.socket == clientSocket)
{
clientList.RemoveAt(nIndex);
break;
}
++nIndex;
}
//Set the text of the message that we will broadcast to all users
msgToSend = msgReceived;
message = msgToSend.ToByte();
foreach (ClientInfo clientInfomsg in clientList)
{
//只发给接受者
if (clientInfomsg.strID == msgToSend.objstrId)
{
//Send the message to all users
clientInfomsg.socket.BeginSend(message, 0, message.Length, SocketFlags.None,
new AsyncCallback(OnSend), clientInfomsg.socket);
}
}
break;
case Command.List:
//同步数据
msgToSend = msgReceived;
//Collect the names of the user in the chat room
foreach (ClientInfo client in clientList)
{
//To keep things simple we use asterisk as the marker to separate the user names
msgToSend.strMessage += client.strID + "*";
}
message = msgToSend.ToByte();
foreach (ClientInfo clientInfolist in clientList)
{
//只发给接受者
if (clientInfolist.strID == msgToSend.strId)
{
//Send the message to all users
clientInfolist.socket.BeginSend(message, 0, message.Length, SocketFlags.None,
new AsyncCallback(OnSend), clientInfolist.socket);
}
}
break;
}
//if (msgToSend.cmdCommand != Command.List || msgToSend.cmdCommand != Command.Chkpws) //List messages are not broadcasted
//{
// message = msgToSend.ToByte();
// foreach (ClientInfo clientInfo in clientList)
// {
// if (clientInfo.socket != clientSocket ||
// msgToSend.cmdCommand != Command.Login)
// {
// //Send the message to all users
// clientInfo.socket.BeginSend(message, 0, message.Length, SocketFlags.None,
// new AsyncCallback(OnSend), clientInfo.socket);
// }
// }
//When a user logs in to the server then we add her to our
//list of clients
msgToSend = msgReceived;
ClientInfo clientInfo = new ClientInfo();
clientInfo.socket = clientSocket;
clientInfo.strID = msgReceived.strId;
clientList.Add(clientInfo);
//Set the text of the message that we will broadcast to all users
msgToSend.strMessage = "<<<" + msgReceived.strId + " 登陆系统>>>";
message = msgToSend.ToByte();
foreach (ClientInfo clientInfologin in clientList)
{
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Server
{
public partial class SGSserverForm : Form
{
//The ClientInfo structure holds the required information about every
//client connected to the server
struct ClientInfo
{
public Socket socket; //Socket of the client
public string strID; //Name by which the user logged into the chat room
}
//The collection of all clients logged into the room (an array of type ClientInfo)
ArrayList clientList;
//The main socket on which the server listens to the clients
Socket serverSocket;
byte[] byteData = new byte[1024];
private byte[] recByte;
private SplitBytes sb;
TcpClient atcpcl = new TcpClient();
public SGSserverForm()
{
Control.CheckForIllegalCrossThreadCalls = false;
clientList = new ArrayList();
InitializeComponent();
}
sb = new SplitBytes();
recByte = new byte[1024];
//try
//{
//We are using TCP sockets
serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
//Assign the any IP of the machine and listen on port number 1000
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 19112);
//clsglo.tcpsrv =new TcpListener(19112);
//clsglo.tcpsrv.Start();
//Bind and listen on the given address
//socket绑定端口
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(4);
//联接对像给SOCKET
//serverSocket = clsglo.tcpsrv.Server;
//联接对像给tcpclist
if (numberOfBytesRead < 1)
{
//If a value less than 1 received that means that client disconnected
clientSocket.Close();
return;
}
// }
sb.AddBytes(byteData, numberOfBytesRead);
byteData = new byte[1024];
if (clientSocket.Available!=0)
{
isFinish = false;
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(Getmsg), clientSocket);
}
if (isFinish)
{
Data msgReceived = new Data(sb.ReceiveAllByte);
OnReceive(msgReceived, clientSocket);
sb.Dispose();
//clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnENDReceive), clientSocket);
//If the user is logging out then we need not listen from her
//如果客户没有退出则维护消息循环
if (msgReceived.cmdCommand != Command.Logout)
{
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(Getmsg), clientSocket);
// //Start listening for more clients
// serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
// //Once the client connects then start receiving the commands from her
// clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
// new AsyncCallback(OnReceive), clientSocket);