110,534
社区成员
发帖
与我相关
我的任务
分享
void Start()
{
//第一个,收发消息
server_Thread = new Thread(ServerStart);
server_Thread.Start();
//第二个,接收图片
InitIMG();
//第三个,发送图片
threadSendIMG = new Thread(SendImg);
threadSendIMG.Start();
}
private void ServerStart()
{
Socket connection = null;
// Socket socketWatch = o as Socket;
while (true)
{
try
{
//接受Andorid信息
connection = server_Socket.Accept();
}
catch (Exception ex)
{
// MessageBox.Show("start error: " + ex.Message);
return;
}
clientep = (IPEndPoint)connection.RemoteEndPoint;
string str = string.Format("connectsuccess,{0},{1}", "", clientep.Address + ":" + clientep.Port);
byte[] data = new byte[1024];
data = Encoding.UTF8.GetBytes(str);
connection.Send(data, data.Length, SocketFlags.None);
client_Thread = new Thread(ReceiveData);
client_Thread.Start(connection);
}
}
private void ReceiveData(object o)
{
Socket socketSend = o as Socket;
Byte[] buffer;
while (true)
{
string message = null;
//将接收到的信息存入到内存缓冲区,并返回其字节数组的长度
try
{
buffer = new Byte[1024];
int length = socketSend.Receive(buffer);
if (length <= 0)
{
string outname = "";
for (int i = 0; i < userList.Count; i++)
{
if (userList[i].usersocket == socketSend)
{
outname = userList[i].username;
userList.RemoveAt(i);
UpdateDGV(userList);
}
}
for (int i = 0; i < userList.Count; i++)
{
string outline = string.Format("logout,{0},{1}", outname, "");
SendtoClient(socketSend, userList[i], outline);
}
socketSend = null;
return;
}
//将机器接受到的字节数组转换为人可以读懂的字符串
message = Encoding.UTF8.GetString(buffer, 0, length);
UpdateTxt(message);
}
catch (Exception ex)
{
}
clientep = (IPEndPoint)socketSend.RemoteEndPoint;
try
{
string[] splitstring = message.Split(',');
IPEndPoint clientIPEndPoint = clientep;
switch (splitstring[0])
{
// 如果是登录信息,向客户端发送应答消息和广播有新用户登录消息
case "login":
User user = new User(splitstring[1], clientIPEndPoint, socketSend, splitstring[3]);
string userItem = user.username + "," + user.userIPEndPoint;
// 往在线的用户列表添加新成员
bool usercontain = false;
int index = 0;
foreach (var l in userList)
{
if (l.username.Equals(user.username))
{
usercontain = true;
index = userList.FindIndex(item => item.Equals(l));
}
}
if (!usercontain)
{
userList.Add(user);
// AddItemToListView(userItem);
UpdateDGV(userList);
}
else
{
}
Thread sendThread = new Thread(SendData);
sendThread.Start(socketSend);
sendThread.Join();
for (int i = 0; i < userList.Count; i++)
{
if (userList[i].username != user.username)
{
SendtoClient(socketSend, userList[i], message);
}
}
break;
case "logout":
for (int i = 0; i < userList.Count; i++)
{
if (userList[i].username == splitstring[1])
{
userList.RemoveAt(i);
UpdateDGV(userList);
}
}
for (int i = 0; i < userList.Count; i++)
{
SendtoClient(socketSend, userList[i], message);
}
break;
case "talk":
string tousername = splitstring[3];
for (int i = 0; i < userList.Count; i++)
{
if (userList[i].username == tousername)
{
SendtoClient(socketSend, userList[i], message);
}
}
break;
}
}
catch
{ }
}
}
public void InitIMG()
{
serverIMGSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint SelfImgIPEndPoint = new IPEndPoint(Dns.GetHostAddresses("")[2], 8888);
serverIMGSocket.Bind(SelfImgIPEndPoint); //绑定IP地址:端口
serverIMGSocket.Listen(20); //设定最多10个排队连接请求
Console.WriteLine("启动监听{0}成功", serverIMGSocket.LocalEndPoint.ToString());
Thread myThread = new Thread(ListenIMGConnect);
myThread.Start();
}
private void ListenIMGConnect()
{
while (true)
{
try
{
Socket clientSocket = serverIMGSocket.Accept();
Thread receiveThread = new Thread(RECVIMG);
receiveThread.Start(clientSocket);
}
catch
{
break;
}
}
}
public static void RECVIMG(object clientSocket)
{
Socket client = clientSocket as Socket;
//获得客户端节点对象
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
//获得[文件名]
string SendFileName = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
//获得[包的大小]
string bagSize = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
//获得[包的总数量]
byte[] dd = TransferFiles.ReceiveVarData(client);
string ss = System.Text.Encoding.UTF8.GetString(dd);
int bagCount = int.Parse(ss);
//获得[最后一个包的大小]
string bagLast = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
string fullPath = Path.Combine(Application.StartupPath + "\\订单\\", SendFileName);
if (File.Exists(fullPath))
return;
//创建一个新文件
FileStream MyFileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
//已发送包的个数
int SendedCount = 0;
while (true)
{
byte[] data = TransferFiles.ReceiveVarData(client);
if (data.Length == 0)
{
break;
}
else
{
SendedCount++;
//将接收到的数据包写入到文件流对象
MyFileStream.Write(data, 0, data.Length);
//显示已发送包的个数
}
}
//关闭文件流
MyFileStream.Close();
//关闭套接字
client.Close();
}