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);
}
}
}
}