关于SOCKET数据通讯的问题(200分少再加)

xhunanpp 2004-08-02 02:40:58
高分求SOCKET数据通讯的源码,
要求一个服务端和N个客务端进行通讯,要求客务端发消息到服务端,服务端到回消息到客务端,

问几个小问题:
1. 当前服务端收到客务端的消息后,怎么能获得当前客务端的IP,
2. 怎样能锁定每一个客务端发送消息到服务端后,服务端会把客务端的消息写入XML文档里,如果N个客务端同时发消息到服务端,哪服务端同时把N个客务端的消息写入XML文档``时会出问题? 怎么解决呢?
有源码吗?

分少了再加```
...全文
501 33 打赏 收藏 举报
写回复
33 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wjy550 2004-09-20
我也要一份,EMAIL:wjy@hfasoft.com
  • 打赏
  • 举报
回复
csq0516 2004-08-12
up
  • 打赏
  • 举报
回复
herony420 2004-08-11
csh420@126.com
谢谢

我觉得你的异常是由于线程间锁定造成的,你应该仔细检查一下
  • 打赏
  • 举报
回复
jun_01 2004-08-11
请教,一个帖子如何发200分啊?
  • 打赏
  • 举报
回复
dsclub 2004-08-11
dsclub@hotmail.com


UP+接分!
  • 打赏
  • 举报
回复
miqier 2004-08-11
楼上留MAIL的都发了。
  • 打赏
  • 举报
回复
gbbword 2004-08-07
to : miqier(米琪儿)
gbbword123@21cn.com
我也想要一份谢谢!
  • 打赏
  • 举报
回复
zhpsam109 2004-08-07
学习!
  • 打赏
  • 举报
回复
Chimeny 2004-08-07
先顶一下
miqier(米琪儿)的程序能否也给我一份Look Look?
Chimeny@163.com
  • 打赏
  • 举报
回复
csq0516 2004-08-06
up
  • 打赏
  • 举报
回复
xhunanpp 2004-08-06
to : miqier(米琪儿)
mail: xhunanpp@163.com

来一份看看,
  • 打赏
  • 举报
回复
bbsuser 2004-08-04
http://www.microsoft.com/china/community/Column/94.mspx
  • 打赏
  • 举报
回复
miqier 2004-08-04
给楼上的两位发了。
  • 打赏
  • 举报
回复
Rockford 2004-08-04
resocman@hotmail.com
多谢!!!
  • 打赏
  • 举报
回复
qunw 2004-08-04
To:miqier(米琪儿)
学习:
wngwz@msn.com
  • 打赏
  • 举报
回复
elite2018 2004-08-03
mark
  • 打赏
  • 举报
回复
daguzhang 2004-08-03
定义的部分的部分长了 贴不出来,有问题的话发短消息我
  • 打赏
  • 举报
回复
miqier 2004-08-03
P2P通信的实例看来符合你的要求,如果需要的话,留下Mail.
  • 打赏
  • 举报
回复
daguzhang 2004-08-03
我刚做了一个网络麻将程序的框架:
private void Form1_Load(object sender, System.EventArgs e)
{
this.menuItem3.Enabled = false;
this.buttonSend.Enabled = false;
this.buttonAccept.Enabled = false;


}

private void button1_Click(object sender, System.EventArgs e)
{
//initialForm();
}



public void setListener()
{
int CurrentSocketNo = flag;
flag++;
try
{
Socket ClientSocket = (Socket)SocketList[CurrentSocketNo];
IPEndPoint epClient = (IPEndPoint)ClientSocket.RemoteEndPoint;
string ClientIP = epClient.Address.ToString();
string ClientPort = epClient.Port.ToString();
String ClientInfo = "Connected with "+ ClientIP + ": " + ClientPort;
this.listConnectedUsers.Items.Add(ClientInfo);
String welcomeMes = "Welcome to server!";
byte[] data = Encoding.Default.GetBytes(welcomeMes);
ClientSocket.Send(data,0,data.Length,SocketFlags.None);
//ClientSocket.Close();
while(true)
{
data = new byte[1024];
int count = ClientSocket.Receive(data);
string getString = Encoding.Default.GetString(data,0,count);
if(count!=0)
{
string revString = Encoding.Default.GetString(data,0,count);
foreach (Socket TempSocket in SocketList)
{
TempSocket.Send(data,0,data.Length,SocketFlags.None);
}
}
else
{
ClientInfo = "Disconnected with "+ ClientIP + ": " + ClientPort;
this.listConnectedUsers.Items.Add(ClientInfo);
ClientSocket.Shutdown(SocketShutdown.Both);
ClientSocket.Close();
SocketList.Remove(ClientSocket);
flag--;
break;

}

}



}
catch(SocketException ex)
{
MessageBox.Show(ex.ToString());
}
return;
}

public void Configuration()
{
try
{
Int32 port = 8088;
IPAddress localAddr =Dns.GetHostByName(Dns.GetHostName()).AddressList[0];//IPAddress.Parse("127.0.0.1");
ep = new IPEndPoint(localAddr,port);
serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
serverSocket.Bind(ep);
serverSocket.Listen(4);
MessageBox.Show("配制服务器成功!");
serverExist = true;
this.menuItem2.Enabled = false;
this.menuItem3.Enabled = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}

SocketList = new ArrayList(4);
flag = 0;
for(int i=0;i<4;i++)
{
Socket ClientSocket = serverSocket.Accept();

SocketList.Add(ClientSocket);
//MessageBox.Show(i.ToString()+" now the flag ="+flag.ToString());
ThreadStart myThreadDelegate = new ThreadStart(setListener);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();

}

}

private void menuItem2_Click(object sender, System.EventArgs e)
{
ThreadStart BeginListen = new ThreadStart(Configuration);
Thread myThread = new Thread(BeginListen);
myThread.Start();


}

public void beginReceive()
{
byte[] data;
while(true)
{
data = new byte[1024];
int count = clientSocket.Receive(data);
string revString = Encoding.Default.GetString(data,0,count);
this.listBoxChatContent.Items.Add(revString);
}
}
public void runClient()
{

string revString = String.Empty;
byte[] data = new byte[1024];
int count = clientSocket.Receive(data);
revString = Encoding.Default.GetString(data,0,count);
this.listBoxChatContent.Items.Add(revString);

ThreadStart BeginListen = new ThreadStart(beginReceive);
ClientRevThread = new Thread(BeginListen);
ClientRevThread.Start();



}
private void button2_Click(object sender, System.EventArgs e)
{
if(this.button2.Text.Trim()=="连接")
{

clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
clientExist = true;
if(this.IpAddress.Text.Trim()!=""&&this.Port.Text.Trim()!="")
{
string ipAddress=IpAddress.Text.Trim();
try
{
IPAddress ip = Dns.Resolve(ipAddress).AddressList[0];
Int32 port=Convert.ToInt32(Port.Text.Trim());
IPEndPoint ep =new IPEndPoint(ip,port);
clientSocket.Connect(ep);
MessageBox.Show("连接服务器成功!");
this.button2.Text="断开";
this.buttonSend.Enabled = true;
this.buttonAccept.Enabled = true;

}
catch
{
MessageBox.Show("连接服务器失败,请确保存在此服务器或服务器已启动!");
}
ThreadStart BeginListen = new ThreadStart(runClient);
ClientMainThread = new Thread(BeginListen);
ClientMainThread.Start();

}
else
{
MessageBox.Show("请填入完整的服务器信息!");
}


}
else
{
//ClientRevThread.Abort();
try
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
clientExist = false;
this.button2.Text="连接";
this.buttonSend.Enabled = false;
this.buttonAccept.Enabled = false;
}
catch
{
MessageBox.Show("关闭连接失败,请确保已经连接上了服务器!");
}

}
}

public void getData()
{


}
private void button3_Click(object sender, System.EventArgs e)
{
String sendString = this.words.Text;
if(sendString!="")
{
byte[] data = Encoding.Default.GetBytes(sendString);
clientSocket.Send(data,0,data.Length,SocketFlags.None);
}

}

private void menuItem3_Click(object sender, System.EventArgs e)
{
try
{

//serverSocket.Shutdown(SocketShutdown.Both);
serverSocket.Close();
this.menuItem2.Enabled = true;
this.menuItem3.Enabled = false;
serverExist = false;
MessageBox.Show("关闭监听成功!");
}
catch
{
MessageBox.Show("关闭监听失败!");
}
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(serverExist)
{
if(serverSocket.Blocking)
serverSocket.Close();
}
if(clientExist)
{
if(clientSocket.Blocking)
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
}


}
}
  • 打赏
  • 举报
回复
tianxiake1 2004-08-03
其实这个问题很好处理。

在服务端监听到客户端的时候,就开启一个线程来处理。而得到的socket实例就是针对这个客户端的。

最近我才做了个这样的程序,效果还可以
  • 打赏
  • 举报
回复
加载更多回复(13)
相关推荐
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2004-08-02 02:40
社区公告

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