用socket接收和发送信息

ruiqiu 2006-01-10 11:47:48
我要实现这样一个功能:客户端要运行程序,必须向服务器端发送消息,服务器端监听客户端,如果客户端有消息发送,接收客户端的MAC地址,再反馈信息给客户端。
没接触过这方面的知识,有点无从下手了,请大家帮帮忙,告诉我应该怎么做啊?
...全文
492 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
conan19771130 2006-03-07
  • 打赏
  • 举报
回复
给个发送类,服务器只能自己编
sing System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace com.zggps
{
/// <summary>
/// SocketClient 的摘要说明。
/// </summary>
public class SocketClient
{
private Socket m_clientSocket;
private int m_nServerPort;
private string m_strServerIP;
private int BufferSize = 1409;
public int BUFFERSIZE
{
get
{
return BufferSize;
}
set
{
BufferSize=value;
}
}
// ManualResetEvent instances signal completion.
private ManualResetEvent connectDone =
new ManualResetEvent(false);
private ManualResetEvent sendDone =
new ManualResetEvent(false);
private ManualResetEvent receiveDone =
new ManualResetEvent(false);

public delegate void OnReceiveMsg(byte[] data);
public event OnReceiveMsg ReceiveMsg;

public delegate void OnConnected();
public event OnConnected Connected;

public delegate void OnDisConnected();
public event OnDisConnected DisConnected;

public delegate void OnSendBack();
public event OnSendBack SendBack;

public delegate void OnError(string msg);
public event OnError ReportError;

private System.Threading.Thread m_mianThread;

private bool m_bConnected=false;

public bool IsConnect
{
get
{
return m_bConnected;
}
set
{
this.m_bConnected = value;
}
}
/// <summary>
/// 创建对象
/// </summary>
/// <param name="strServerIP">Server IP Address</param>
/// <param name="nPort">Server Port</param>
public SocketClient(string strServerIP,int nPort)
{
this.m_strServerIP = strServerIP;
this.m_nServerPort = nPort;
}

/// <summary>
/// Start the client socket
/// </summary>
public void Begin()
{
System.Threading.ThreadStart ts=new ThreadStart(this.StartClient);
this.m_mianThread = new Thread(ts);
this.m_mianThread.Start();
}

/// <summary>
/// 清除正在使用的资源
/// </summary>
public void Dispose()
{
if(this.m_clientSocket.Connected)
{
this.m_clientSocket.Shutdown(SocketShutdown.Both);
this.m_clientSocket.Close();
}
if(this.m_mianThread.IsAlive)
this.m_mianThread.Abort();
}

private void StartClient()
{
// Connect to a remote device.
try
{
IPHostEntry ipHostInfo = Dns.Resolve(this.m_strServerIP);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, this.m_nServerPort);

// Create a TCP/IP socket.
this.m_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

// Connect to the remote endpoint.
this.m_clientSocket.BeginConnect( remoteEP,
new AsyncCallback(ConnectCallback), this.m_clientSocket);
}
catch (Exception e)
{
this.ReportError("StartClient found a error:\r\n" + e.ToString());
}
}

private void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket) ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
this.m_bConnected = true;
this.Connected();
Receive(client);

}
catch (Exception e)
{
this.ReportError("连接到远程服务器失败,请检查网络连接,错误内容如下:\r\n"+e.ToString());
this.m_bConnected =false;
}
}

/// <summary>
/// 关闭连接
/// </summary>
public void close()
{
try
{
if(m_clientSocket.Connected)
{
this.m_clientSocket.Shutdown(SocketShutdown.Both);
this.m_clientSocket.Close();
}
}
catch(Exception e)
{
this.ReportError("Close Client Error:" + e.ToString());
}
finally
{
this.m_bConnected = false;
}
}

private void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
state.buffer=new byte[BufferSize];
// Begin receiving the data from the remote device.
client.BeginReceive( state.buffer, 0, BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
this.ReportError("Begin Receive found a error:\r\n" + e.ToString());
}
}

protected void ReceiveCallback( IAsyncResult ar )
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket;

// Read data from the remote device.
int bytesRead = client.EndReceive(ar);

if (bytesRead > 0)
{
byte[] data=new byte[bytesRead];
for(int i=0;i<bytesRead;i++)
{
data[i]=state.buffer[i];
}
this.ReceiveMsg(data);
// Get the rest of the data.
client.BeginReceive(state.buffer,0,BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
this.DisConnected();
this.close();
}
}
catch
{
//this.ReportError("Receive msg found a error:\r\n" + e.ToString());
this.close();
this.DisConnected();
}
}

/// <summary>
/// 发送信息到服务器
/// </summary>
/// <param name="data">信息内容</param>
public void Send(byte[] data)
{
try
{
if(this.m_clientSocket.Connected)
this.m_clientSocket.BeginSend(data,0,data.Length,0,
new AsyncCallback(SendCallback),this.m_clientSocket);
else
{
this.ReportError("发送信息错误,连接已经断开");
}
}
catch
{
this.ReportError("发送信息错误");
this.close();
}
}

private void Send(Socket client, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);

// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}

private void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket) ar.AsyncState;

// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
this.SendBack();
}
catch (Exception e)
{
ReportError(e.ToString());
}
}
}

// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Receive buffer.
public byte[] buffer;
}
}
ddltxj 2006-03-07
  • 打赏
  • 举报
回复
无法将文件“WindowsService1.exe”复制到运行目录。进程无法访问文件,因为另一个程序正在使用此文件;
无法将文件“WindowsService1.pdb”复制到运行目录。进程无法访问文件,因为另一个程序正在使用此文件。


你的WindowsService1.exe正在运行,将进程杀死,重新编译
goldenroses 2006-03-07
  • 打赏
  • 举报
回复
只是最基本的socket通信,好好研究吧
TomKen 2006-03-07
  • 打赏
  • 举报
回复
market
wwbjt 2006-02-13
  • 打赏
  • 举报
回复
去找一下这个教程“c#网络核心编程”上面有详细的例子。
yingfeiqiyue 2006-02-13
  • 打赏
  • 举报
回复
文件是否只读??
ruiqiu 2006-01-12
  • 打赏
  • 举报
回复
先不要考虑其他的,能否先告诉我server和client之间是怎么样传递和接受信息的吧?
不知道怎么弄,好难受
ruiqiu 2006-01-12
  • 打赏
  • 举报
回复
运行时,出现这样的错误,是什么原因:未能将临时文件复制到输出目录中;
无法将文件“WindowsService1.exe”复制到运行目录。进程无法访问文件,因为另一个程序正在使用此文件;
无法将文件“WindowsService1.pdb”复制到运行目录。进程无法访问文件,因为另一个程序正在使用此文件。




ybzsu 2006-01-11
  • 打赏
  • 举报
回复
其他的都好办,不过这个MAC就有让人头痛的了,因为如果不是client主动发自己MAC的话靠服务器获取的话就太难了
jingjing_180 2006-01-11
  • 打赏
  • 举报
回复
我也正在做这个东东,不过我要实现的功能比你多了哦
学习中。。。。。。
李天平 2006-01-11
  • 打赏
  • 举报
回复
异步Socket通信总结
http://ltp.cnblogs.com/archive/2006/01/10/289680.html
ruiqiu 2006-01-11
  • 打赏
  • 举报
回复
着急中,请各位帮帮忙啊
jiezhi 2006-01-11
  • 打赏
  • 举报
回复
http://jiezhi.cnblogs.com/archive/2005/08/15/215419.html

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧