using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace MySocketServerCon
{
public class Serverlicten
{
IPAddress ipadr;
IPEndPoint ipep;
Socket serverSocket;
Socket clientSocket;
int port = 8888;
//客户端线程
Thread clientThread;
public void Start()
{
ipadr = IPAddress.Parse("127.0.0.1");
ipep = new IPEndPoint(ipadr, port);
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定终结点
serverSocket.Bind(ipep);
serverSocket.Listen(10);//排队10个
Console.WriteLine("listening 127.0.0.1:8888 ");
//侦听
while (true)
{
try
{
clientSocket = serverSocket.Accept();
//来一个启动一个线程
ThreadStart ts = new ThreadStart(RecievData);
clientThread = new Thread(ts);
clientThread.Start();
}
catch (Exception ex)
{
Console.WriteLine("listening Error: " + ex.Message);
}
}
}
void RecievData()
{
Socket s = this.clientSocket;
//根据受到客户端的套接字发送返回消息
IPEndPoint clientep = (IPEndPoint)s.RemoteEndPoint;
//记录请求IP
//lstServer.Items.Add("Client:" + clientep.Address + "(" + clientep.Port + ")");
Console.WriteLine("Client:" + clientep.Address + "(" + clientep.Port + ")");
//***************************************************************************
//回发消息
string welcome = "Welcome to my sever";
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(welcome);
//发给客户端
s.Send(data);
//***************************************************************************
//在套接字上接收客户端发送的信息
bool keepalive = true;
byte[] buffer = new byte[1024];
while (keepalive)
{
int bufLen = 0;
try
{
//获取已经从网络接收且可供读取的数据量。
bufLen = s.Available;
//获取信息
s.Receive(buffer, 0, bufLen, SocketFlags.None);
//继续等
if (bufLen == 0)
continue;
}
catch (Exception ex)
{
Console.WriteLine("Receive Error:" + ex.Message);
return;
}
//记录收到的信息i
clientep = (IPEndPoint)s.RemoteEndPoint;
string clientcommand = System.Text.Encoding.ASCII.GetString(buffer).Substring(0, bufLen);
//lstServer.Items.Add(clientcommand + "(" + clientep.Address + ":" + clientep.Port + ")");
Console.WriteLine(clientcommand + "(" + clientep.Address + ":" + clientep.Port + ")");
//回发收到的消息
data = Encoding.ASCII.GetBytes(clientep.Address +" Say:"+clientcommand);
//发给客户端
s.Send(data);
}
}
}
}
===========================================
客户端
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 MySocketClient
{
public partial class Form1 : Form
{
//代理控件
delegate void listboxDel(string s);
listboxDel listboxdel;
void LoadListBox(string s)
{
lstClient.Items.Add(s);
}
public Form1()
{
InitializeComponent();
listboxdel = new listboxDel(LoadListBox);
}
Socket clientSocket;
byte[] data = new byte[1024];
private void Form1_Load(object sender, EventArgs e)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//将套接字与远程服务器地址相连
try
{
clientSocket.Connect(ipep);
}
catch (SocketException ex)
{
MessageBox.Show("connect error: " + ex.Message);
return;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(txtClient.Text);
clientSocket.Send(data, data.Length, SocketFlags.None);
}
catch (Exception ex)
{
MessageBox.Show("Receive Error:" + ex.Message);
return;
}
//接受信息
while (true)
{
int bufLen = 0;
try
{
bufLen = clientSocket.Available;//返回内容的大小
if (bufLen == 0)
break ;
clientSocket.Receive(data, 0, bufLen, SocketFlags.None);
//if (bufLen == 0)
// continue;
}
catch (Exception ex)
{
MessageBox.Show("Receive Error:" + ex.Message);
return;
}
//加入
string clientcommand = System.Text.Encoding.ASCII.GetString(data).Substring(0, bufLen);
//lstClient.Items.Add(clientcommand);
lstClient.Invoke(listboxdel, clientcommand);
}
}
}
}