服务端:

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.Sockets;
using System.Net;
using System.Threading;
using System.IO;

namespace 服务端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Control.CheckForIllegalCrossThreadCalls = false;
}

/// <summary>
/// 监听socket
/// </summary>
Socket socketListen;

Dictionary<string, Socket> dirSocket = new Dictionary<string, Socket>();


//户务端监听客户端连接情况
private void btnListen_Click(object sender, EventArgs e)
{
try
{
socketListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPAddress ip = IPAddress.Any;

IPEndPoint point = new IPEndPoint(ip, Int32.Parse(txtPort.Text));

socketListen.Bind(point);

socketListen.Listen(10);

showMsg("监听成功");



Thread thread = new Thread(Listen);

thread.IsBackground = true;

thread.Start();
}
catch (Exception)
{

}


}

/// <summary>
/// 监听客服端
/// </summary>
void Listen()
{

try
{
//不断的监听客户端
while (true)
{
Socket socket = socketListen.Accept();

showMsg(socket.RemoteEndPoint.ToString() + ":连接成功");

cbList.Items.Add(socket.RemoteEndPoint.ToString());

dirSocket.Add(socket.RemoteEndPoint.ToString(), socket);

//不断接受客户端信息
Thread thread = new Thread(Receive);

thread.IsBackground = true;

thread.Start(socket);

}

}
catch (Exception)
{


}

}
/// <summary>
/// 接收客服端的信息
/// </summary>
/// <param name="o"></param>
void Receive(object o)
{
try
{
Socket socketSend = o as Socket;
byte[] buffer = new byte[1024 * 1024 * 2];

while (true)
{
int r = socketSend.Receive(buffer);

if (r <= 0)
{
break;
}

string result = Encoding.UTF8.GetString(buffer, 0, r);

showMsg(socketSend.RemoteEndPoint.ToString() + ":" + result);


}
}
catch (Exception)
{


}

}


/// <summary>
/// 显示信息
/// </summary>
/// <param name="msg"></param>
void showMsg(string msg)
{
txtLog.AppendText(msg + "\r\n");
}

/// <summary>
/// 发送信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSendMsg_Click(object sender, EventArgs e)
{
string ipAddress = cbList.SelectedItem.ToString();

if (string.IsNullOrEmpty(ipAddress))
{
return;
}

string msg = this.txtMsg.Text.Trim();

byte[] msgBytes = Encoding.UTF8.GetBytes(msg);

byte[] newArray = GetByteArray(0,msgBytes);

dirSocket[ipAddress].Send(newArray);

}

/// <summary>
/// 选择文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = ofd.FileName;
}

}



/// <summary>
/// 发送文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSendFile_Click(object sender, EventArgs e)
{
string ipAddress = cbList.SelectedItem.ToString();

if (string.IsNullOrEmpty(ipAddress))
{
return;
}

string filename = this.txtPath.Text.Trim();

byte[] msgBytes = File.ReadAllBytes(filename);

byte[] newArray = GetByteArray(1, msgBytes);

dirSocket[ipAddress].Send(newArray);
}

/// <summary>
/// 发送抖屏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnMoveScreen_Click(object sender, EventArgs e)
{
string ipAddress = cbList.SelectedItem.ToString();

if (string.IsNullOrEmpty(ipAddress))
{
return;
}

byte[] newArray = new byte[1] { 2 };

dirSocket[ipAddress].Send(newArray);
}

/// <summary>
/// 处理发送byte数据
/// </summary>
/// <param name="type"></param>
/// <param name="array"></param>
/// <returns></returns>
private byte[] GetByteArray(int type,byte[] array)
{
byte[] newArray = new byte[array.Length+1];

array.CopyTo(newArray, 1);

switch (type)
{
case 0:
newArray[0] = 0;
break;
case 1:
newArray[0] = 1;
break;
default:
newArray[0] = 2;
break;
}

return newArray;
}



}
}


客服端:

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.Sockets;
using System.Net;
using System.Threading;
using System.IO;

namespace 客户端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Control.CheckForIllegalCrossThreadCalls = false;
}

Socket socket;

private void btnListen_Click(object sender, EventArgs e)
{
try
{

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPAddress ip = IPAddress.Parse(txtAddress.Text);

IPEndPoint point = new IPEndPoint(ip, Int32.Parse(txtPort.Text));

socket.Connect(point);

showMsg("连接服务端成功!!!!");

Thread thread = new Thread(Receive);

thread.IsBackground = true;


thread.SetApartmentState(ApartmentState.STA);

thread.Start();
}
catch (Exception ex)
{


}
}

void Receive()
{
try
{

byte[] buffer = new byte[1024 * 1024 * 2];

while (true)
{


int r = socket.Receive(buffer);
if (r <= 0) break;

switch (buffer[0])
{
case 0 : //文本内容
GetMsg(buffer, r);
break;
case 1: //文本文件
GetFile(buffer, r);
break;

default: //抖屏
GetMoveScreen();
break;
}




}
}
catch (Exception)
{


}
}

/// <summary>
/// 接收内容
/// </summary>
/// <param name="array"></param>
/// <param name="realLength"></param>
void GetMsg(byte[] array,int realLength)
{
string msg = Encoding.UTF8.GetString(array, 1, realLength-1);

showMsg(msg);
}
/// <summary>
/// 接收文件
/// </summary>
/// <param name="array"></param>
/// <param name="realLength"></param>
void GetFile(byte[] array,int realLength)
{
SaveFileDialog sfd = new SaveFileDialog();

if (sfd.ShowDialog() == DialogResult.OK)
{
string filename = sfd.FileName;

using(FileStream fs = new FileStream(filename,FileMode.Create,FileAccess.ReadWrite))
{
fs.Write(array, 1, realLength - 1);
}

showMsg("文件保存到:"+filename+"成功!!!");
}
}
Random r = new Random();
/// <summary>
/// 抖屏
/// </summary>
void GetMoveScreen()
{
Point startPoint = this.Location;

for (int i = 0; i < 200; i++)
{
this.Location = new Point(startPoint.X + r.Next(20), startPoint.Y + r.Next(20));
}

this.Location = startPoint;
}

/// <summary>
/// 显示内容
/// </summary>
/// <param name="p"></param>
private void showMsg(string p)
{
textBox1.AppendText(p + "\r\n");
}


/// <summary>
/// 发送内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSend_Click(object sender, EventArgs e)
{
try
{
string msg = txtMsg.Text.Trim();

byte[] buffer = Encoding.UTF8.GetBytes(msg);

socket.Send(buffer);
}
catch (Exception)
{


}
}



}
}


 socket 聊天室_ipad