客户端代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;namespace socketC
{
public partial class Form1 : Form
{
Socket mSocket;
StateObject state;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = Dns.GetHostAddresses("localhost")[0];
IPEndPoint ipe = new IPEndPoint(ip,0);
mSocket.Bind(ipe);
mSocket.Connect("192.168.1.110",int.Parse(this.textBox2.Text));
state = new StateObject();
state.workSocket = mSocket;
mSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
} private void ReceiveCallback(IAsyncResult ar)
{
try
{
//
StateObject state = ar.AsyncState as StateObject;
//读取数据
int bytesRead = mSocket.EndReceive(ar);
if (bytesRead > 0)
{
// state.sb.Append(UTF8Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); string s = UTF8Encoding.UTF8.GetString(state.buffer, 0, bytesRead);
string sb = s.ToString();
this.Invoke((EventHandler)(delegate
{
// MessageBox.Show(this, "服务器关闭");
this.richTextBox1.Text += sb;
}));
mSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
}
catch (SocketException se)
{
}
catch (Exception e)
{
throw new ApplicationException(e.Message);
} } private void button2_Click(object sender, EventArgs e)
{
Byte[] SendData=UTF8Encoding.UTF8.GetBytes(this.textBox3.Text);
if (mSocket == null)
throw new ArgumentNullException("连接不存在");
if (SendData == null)
return;
mSocket.BeginSend(SendData, 0, SendData.Length, 0, new AsyncCallback(SendCallBack), mSocket);
// mSocket.s
}
private void SendCallBack(IAsyncResult ar)
{
try
{
mSocket.EndSend(ar);
this.Invoke((EventHandler)(delegate
{ this.richTextBox1.Text += this.textBox3.Text;
this.textBox3.Text = "";
}));
}
catch (SocketException se)
{
throw new ApplicationException(se.Message);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;namespace socketC
{
public partial class Form1 : Form
{
Socket mSocket;
StateObject state;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = Dns.GetHostAddresses("localhost")[0];
IPEndPoint ipe = new IPEndPoint(ip,0);
mSocket.Bind(ipe);
mSocket.Connect("192.168.1.110",int.Parse(this.textBox2.Text));
state = new StateObject();
state.workSocket = mSocket;
mSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
} private void ReceiveCallback(IAsyncResult ar)
{
try
{
//
StateObject state = ar.AsyncState as StateObject;
//读取数据
int bytesRead = mSocket.EndReceive(ar);
if (bytesRead > 0)
{
// state.sb.Append(UTF8Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); string s = UTF8Encoding.UTF8.GetString(state.buffer, 0, bytesRead);
string sb = s.ToString();
this.Invoke((EventHandler)(delegate
{
// MessageBox.Show(this, "服务器关闭");
this.richTextBox1.Text += sb;
}));
mSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
}
catch (SocketException se)
{
}
catch (Exception e)
{
throw new ApplicationException(e.Message);
} } private void button2_Click(object sender, EventArgs e)
{
Byte[] SendData=UTF8Encoding.UTF8.GetBytes(this.textBox3.Text);
if (mSocket == null)
throw new ArgumentNullException("连接不存在");
if (SendData == null)
return;
mSocket.BeginSend(SendData, 0, SendData.Length, 0, new AsyncCallback(SendCallBack), mSocket);
// mSocket.s
}
private void SendCallBack(IAsyncResult ar)
{
try
{
mSocket.EndSend(ar);
this.Invoke((EventHandler)(delegate
{ this.richTextBox1.Text += this.textBox3.Text;
this.textBox3.Text = "";
}));
}
catch (SocketException se)
{
throw new ApplicationException(se.Message);
}
}
}
}
解决方案 »
- LINQ to SQL 中的DataContext方法及记录的存储
- 有关数组的问题,求指教~
- 请帮忙看看修改区域语言后,实时生效的方法.为何无效?
- 搭配开发环境问题
- 关于 SortedList 的问题 紧急...
- gridview 如何根据给定参数绑定dataset中的某个表
- gridview的分页问题 在asp.net vs2005 c# datasourceid 是objectdatasource
- .net下开发视频会议,大家提供些资料给俺啊
- 关于用户控件的问题! 请前辈指导,先感谢前辈了!
- c#调用Matlab出错 --找不到指定模块,,求指导??
- 小弟咨询个关于C#自动发邮件问题
- C#制作安装程序如何把第三方程序附加进去
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;namespace SocketS
{
public partial class Form1 : Form
{ SortedList<string, Socket> clients = new SortedList<string, Socket>();
StateObject state;
public Form1()
{
InitializeComponent();
}
Socket socket = null;
byte[] buffer = new byte[256];
private void button1_Click(object sender, EventArgs e)
{
state = new StateObject();
// socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
state.workSocket = socket;
IPAddress ip = Dns.GetHostAddresses("192.168.1.110")[0];
IPEndPoint ipe = new IPEndPoint(ip, 2020);
socket.Bind(ipe);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptCallBack), socket);//异步
// socket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
} int count = 0;
//public void BeginAcceptData()
//{
// count++;
// if (socket == null)
// throw new ArgumentNullException("连接对象为空");
// //开始接收数据
// state.workSocket = socket;
// clients.Add(count.ToString(),socket);
// socket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
// //receiveDone.WaitOne();
//} private void AcceptCallBack(IAsyncResult ar)
{
count++;
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
// state = new StateObject();
state.workSocket = handler;
this.Invoke((EventHandler)(delegate
{
// (clientSock.RemoteEndPoint as IPEndPoint).IPAddress
this.listBox1.Items.Add((handler.RemoteEndPoint as IPEndPoint).Address + ":" + (handler.RemoteEndPoint as IPEndPoint).Port);
}));
clients.Add(count.ToString(), listener);
// MessageBox.Show((handler.RemoteEndPoint as IPEndPoint).Address.ToString() + (handler.RemoteEndPoint as IPEndPoint).Port.ToString());
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
//
StateObject state = ar.AsyncState as StateObject;
//读取数据
int bytesRead = state.workSocket.EndReceive(ar);
if (bytesRead > 0)
{
// string s=
string sb =UTF8Encoding.UTF8.GetString(state.buffer, 0, bytesRead);
//if (sb.Substring(sb.Length - 1, 1) == EndChar)
//{
//接收完成
//激发事件
//if (onStreamData != null)
// onStreamData(mID, UTF8Encoding.UTF8.GetBytes(sb));
// if (onStringData != null)
// onStringData(mID, sb);
//
// state = new StateObject();
// state.workSocket = mSocket;
// }
// Get the rest of the data.
this.Invoke((EventHandler)(delegate
{
// MessageBox.Show(this, "服务器关闭");
this.richTextBox1.Text += sb;
}));
state.sb = null;
state.workSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
}
catch (SocketException se)
{
//if (onClosed != null)
// onClosed(ID, se.Message);
}
catch (Exception e)
{
throw new ApplicationException(e.Message);
} } private void button2_Click(object sender, EventArgs e)
{ //for(int i=0;i<clients.Count;i++)
//{
// MessageBox.Show(clients.Values[i].RemoteEndPoint.ToString());
//}
Byte[] SendData = UTF8Encoding.UTF8.GetBytes(this.textBox1.Text);
if (state.workSocket == null)
throw new ArgumentNullException("连接不存在");
if (SendData == null)
return;
//请教这个是能实现信息转发吗,就是说信息发给所有连接在这个服务器的客户端,是这样发的吗,怎么没按下按钮没反映,
//但换成下面那句就行,但只能跟第一个连接相互通讯,我要实现跟所有连接的客户端通讯,怎么改,如果是要实现跟指定的客户端通讯(从listbox中选择一个连接在这个服务器上的客户端),又怎么实现,麻烦各位高手瞧瞧,调试调试,指点迷津,
不胜感激 // for (int i = 0; i < clients.Count; i++)
//{
// clients.Values[i].BeginSend(SendData, 0, SendData.Length, 0, new AsyncCallback(SendCallBack), clients.Values[i]);
// } state.workSocket.BeginSend(SendData, 0, SendData.Length, 0, new AsyncCallback(SendCallBack), state.workSocket); // AcceptedSocket.LinkObject.RemoteEndPoint as IPEndPoint).Address // UdpClient udpClient = new UdpClient();
// //初始化一个ip地址类的实例
//// IPAddress ipAddress = IPAddress.Parse("192.168.1.110");
// //UdpClient的连接方法,有两个参数,一个是ip地址,一个是端口
// udpClient.Connect((clients.Values[0].RemoteEndPoint as IPEndPoint).Address, (clients.Values[0].RemoteEndPoint as IPEndPoint).Port);
// //把要发送的数据用UTF8编码成字节数组
// byte[] sendData = Encoding.UTF8.GetBytes(textBox1.Text);
// udpClient.Send(sendData, sendData.Length);
}
private void SendCallBack(IAsyncResult ar)
{
try
{
state.workSocket.EndSend(ar);
this.Invoke((EventHandler)(delegate
{ this.richTextBox1.Text += this.textBox1.Text;
this.textBox1.Text = "";
}));
}
catch (SocketException se)
{
throw new ApplicationException(se.Message);
}
//catch (Exception e)
//{
// throw new ApplicationException(e.Message);
//}
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}
怎么这个应用只能够实现一对一的通讯,如果在启动一个客户端的程序,连接发送信息没有用,服务器也不能发送信息过去,
在这个基础上怎么去实现,一个服务器,多台客户机之间的通讯,如果服务器要指定(从listbox中选择一个连接在这个服务器上的客户端)选择一个客户端,实现一对一通讯,如果不选择就跟listbox中所有的连接通讯,怎么去实现,指点迷津,不胜感激,最好把调试过后的代码贴上,参考参考,谢谢!!!!!
这个不是保存了连接吗?,至于管理?
对这方面了解不太多
http://download.csdn.net/source/2821308
邮箱地址:[email protected]
[email protected]
邮箱地址:[email protected]
谢谢
邮箱地址:[email protected]
谢谢