初学 Socket 编程!!求救!!

syma7788 2003-10-22 11:21:53
各位 我初学 Socket 编程,但任务时间紧迫,那位有 多客户端异步传输 文件的例子,请发一份!不胜感激!高分回报(100)!
sy_ma@126.com
...全文
18 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhlcss 2003-10-22
  • 打赏
  • 举报
回复
public class AsyncSocket
{
public bool connected = false;
private Socket m_sock;
private const string CRLF = "\r\n";
#region AsyncSocket()
public AsyncSocket()
{

}
#endregion

#region Connect(string hostname, int port)
public void Connect(string hostname, int port)
{
try
{
IPHostEntry ipHostInfo = Dns.Resolve(hostname);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

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

m_sock.BeginConnect( remoteEP,
new AsyncCallback(ConnectCallback), m_sock);

}
catch (SocketException e)
{
MessageBox.Show(e.Message);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
#endregion

#region ConnectCallback(IAsyncResult ar)
private void ConnectCallback(IAsyncResult ar)
{
try
{
Socket sock = (Socket) ar.AsyncState;

sock.EndConnect(ar);

connected = true;
}
catch (SocketException e)
{
ErrorNotify(e.Message, e.ErrorCode);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
#endregion

#region CheckString(InfoStruct info)
public void CheckString(InfoStruct info)
{
switch(info.flag)
{
case "login":
UserInfo ui = new UserInfo();
ui.Logined = Convert.ToBoolean(info.parameter1);
break;
case "message":
MessageInfo mi = new MessageInfo();

break;
default:
break;
}
MessageBox.Show(info.parameter1);
}
#endregion
#region Receive()
public void Receive()
{
try
{
StateObject state = new StateObject();
state.workSocket = m_sock;
m_sock.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
#endregion

#region ReceiveCallback( IAsyncResult ar )
private void ReceiveCallback( IAsyncResult ar )
{
try
{
StateObject state = (StateObject) ar.AsyncState;
Socket sock = state.workSocket;
string response = string.Empty;

int bytesRead = sock.EndReceive(ar);

// if (bytesRead > 0)
// {
// string s = Encoding.ASCII.GetString(state.buffer,0,bytesRead);
// state.sb.Append(s);
//
// response = state.sb.ToString();
// if (response.EndsWith(CRLF))
// {
// ReceiveResponse(response, state);
// state.sb.Remove(0, state.sb.Length);
// }
// }

if (bytesRead > 0)
{
sock.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(state.buffer);
InfoStruct info = (InfoStruct)bf.Deserialize(ms);
CheckString(info);
}
}
catch (ObjectDisposedException e)
{
MessageBox.Show(e.ToString());
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
#endregion

#region ReceiveResponse(string response, StateObject state)
private void ReceiveResponse(string response, StateObject state)
{
string scode = string.Empty;
ArrayList lResponse = new ArrayList();

int i, j;
int maxline = 0;

i = 0;
do
{
j = response.IndexOf(CRLF, i);
if (j > -1)
{
lResponse.Add (response.Substring(i, j - i));
i = j + 2;
}
else
{
lResponse.Add (response.Substring(i, response.Length - i));
}
maxline++;
} while (i < response.Length);

int idline = 0;
int idstart = 0;

do
{
if (((string)lResponse[idline]).Length > 3)
{
if (((string)lResponse[idline])[3] == '-' || state.multires.Length > 0)
{
if (state.multires.Length == 0)
{
state.multires.Append ( lResponse[idline]);
state.scode = ((string)lResponse[idline]).Substring(0,3);
idstart = idline + 1;
}
else
idstart = idline;
for (j = idstart; j < lResponse.Count; j++)
{
state.multires.Append( lResponse[j]);
if (((string)lResponse[j]).Length > 3)
if (((string)lResponse[j]).Substring(0,3) == state.scode && ((string)lResponse[j])[3] != '-')
break;
}
if (j < lResponse.Count)
{
state.multires.Remove(0, state.multires.Length);
}
idline = j + 1;
}
else
{
idline++;
}
}
else
idline++;
} while (idline < lResponse.Count);
}
#endregion

public void Send(string data)
{
try
{

byte[] byteData = Encoding.ASCII.GetBytes(data);

m_sock.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), m_sock);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}

public void Login(string account, string pwd)
{
try
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
InfoStruct info = new InfoStruct();
info.flag = "login";
info.parameter1 = account;
info.parameter2 = pwd;
bf.Serialize(ms, info);
byte[] byteData = ms.ToArray();

if (byteData.Length > 0)
m_sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), m_sock);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}

public struct member
{
public string account;
public string pwd;
}

private void SendCallback(IAsyncResult ar)
{
try
{
Socket sock = (Socket) ar.AsyncState;

int bytesSent = sock.EndSend(ar);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
#region Close()
public void Close()
{
try
{
m_sock.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
#endregion

#region
private void ErrorNotify(string errMsg, int errCode)
{
switch(errCode)
{
case 10061:
MessageBox.Show("对不起,无法连接到服务器,请检查服务器地址是否正确!","错误",
MessageBoxButtons.OK);
break;
default:
MessageBox.Show(errCode.ToString() + ":" + errMsg);
break;
}
}
#endregion
}
rgbcn 2003-10-22
  • 打赏
  • 举报
回复
see Socket Programming in C# - Part 2 - Getting Started

http://www.developerfusion.com/show/3997/2/
dingyy 2003-10-22
  • 打赏
  • 举报
回复
up
rockrabbit 2003-10-22
  • 打赏
  • 举报
回复
我帮你up。
snowxu 2003-10-22
  • 打赏
  • 举报
回复
gz
syma7788 2003-10-22
  • 打赏
  • 举报
回复
还是复杂了,最好是能有比较实际的应用例子!!谢谢!!!
seaAsky 2003-10-22
  • 打赏
  • 举报
回复
up

110,530

社区成员

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

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

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