c#.net tcp客户端和服务器应用程序

问题描述:

大家好

我在编写应用程序时需要一些帮助.它的系统托盘应用程序可监视服务.当用户启动服务时,它应该向所有已打开该应用程序的用户发送通知,该人x正在启动服务(应出现一个通知气泡).同一网络上的所有用户

该应用程序必须是客户端和服务器应用程序.当应用打开时,服务器部分启动.当用户启动服务时,客户端部分会踢出并发送消息.

有一个带用户名,机器名等的数据库.

有人可以帮忙吗?我不熟悉TCP客户端和服务器的内容.




更新:

好的,这就是我到目前为止所得到的,但是在成功发送消息后,我收到了错误消息
通常在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)中仅允许使用每个套接字地址(协议/网络地址/端口)

公共无效Server()
{

//尝试
//{
//IPAddress []地址列表= Dns.GetHostAddresses(Environment.MachineName);
//foreach(地址列表中的IPAddress地址)
//{
//myIP =(theaddress.ToString());
//MessageBox.Show(myIP);
//}

//这需要是您的ipadress
IPAddress ipAd = IPAddress.Parse("); //使用本地m/c IP地址,并在客户端中使用相同的地址
TcpListener myList =新的TcpListener(ipAd,8001);
myList.Start();
//MessageBox.Show(服务器正在端口8001上运行...");
//MessageBox.Show(本地端点为:" + myList.LocalEndpoint);
//MessageBox.Show(正在等待连接.....");
套接字s = myList.AcceptSocket();
//MessageBox.Show(接受的连接来自" + s.RemoteEndPoint);
byte [] b =新的byte [100];
int k = s.Receive(b);
//MessageBox.Show("Receiveved ...);
字符ms;
字符串ms2 =";
for(int i = 0; i< k; i ++)
{
ms =(Convert.ToChar(b [i]));
ms2 = ms2 + ms;
}
//if(ms2.Substring(0,5)=="notify")
//{
//ms2 = ms2.Substring(0,6);
//}
Form1 fmg =新的Form1();
fmg.notifyIcon1.ShowBalloonTip(5000,"Attention",ms2,ToolTipIcon.Info);
//MessageBox.Show(ms2); //客户端发回的邮件
ASCIIEncoding asen =新的ASCIIEncoding();
//s.Send(asen.GetBytes(该字符串已由服务器接收.")); //向客户端发送确认消息
//MessageBox.Show("\\ n已发送确认");
s.Close();
myList.Stop();


//}
//catch(异常e)
//{
//MessageBox.Show("Error ....." + e.StackTrace);
}
//}

公共无效的客户端(字符串notifymessage)
{
试试
{
TcpClient tcpclnt =新的TcpClient();
//MessageBox.Show("Connecting .....);
//您要将邮件发送到的ipadress
//需要存储在数据库中,循环发送给所有用户
tcpclnt.Connect(",8001); //在服务器程序中使用ipaddress
//MessageBox.Show("Connected");
//MessageBox.Show(输入要传输的字符串:");
//字符串str = Console.ReadLine();
字符串str = notifymessage;
流stm = tcpclnt.GetStream();
ASCIIEncoding asen =新的ASCIIEncoding();
byte [] ba = asen.GetBytes(str);
//MessageBox.Show("Transmitting .....);
stm.Write(ba,0,ba.Length);
byte [] bb =新的byte [100];
int k = stm.Read(bb,0,100);
char ms;
字符串ms2 =";
for(int i = 0; i< k; i ++)
{
ms =(Convert.ToChar(bb [i]));
ms2 = ms2 + ms;
}

//MessageBox.Show(ms2);
tcpclnt.Close();

}
catch(异常e)
{
MessageBox.Show("Error ....." + e.StackTrace);
}
}





谢谢

Lee

Hey Guys

I need some help with an app im writing. Its system tray app that monitors a service. when the user starts the service it should send a notification to all users who have the app opened, that person x is starting the service(a notification bubble should appear). All users on the same network

The app needs to be a client and server app. Server part starts up when the app opens. when a user starts the service, the client part kicks in,sends the message.

There is a db with usernames,machinenames etc.

Can someone please assist. Im not familar with TCP Client and Server stuff.




UPDATE:

ok so this is what i got so far, but im getting an error after a successfull message send
Only one usage of each socket address (protocol/network address/port) is normally permitted at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)

public void Server()
{

//try
//{
//IPAddress[] addresslist = Dns.GetHostAddresses(Environment.MachineName);
//foreach (IPAddress theaddress in addresslist)
//{
// myIP = (theaddress.ToString());
// MessageBox.Show(myIP);
//}

//this needs to be your ipadress
IPAddress ipAd = IPAddress.Parse(""); //use local m/c IP address, and use the same in the client
TcpListener myList = new TcpListener(ipAd, 8001);
myList.Start();
// MessageBox.Show("The server is running at port 8001...");
//MessageBox.Show("The local End point is :" + myList.LocalEndpoint);
//MessageBox.Show("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
//MessageBox.Show("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
//MessageBox.Show("Recieved...");
char ms;
string ms2 = "";
for (int i = 0; i < k; i++)
{
ms = (Convert.ToChar(b[i]));
ms2 = ms2 + ms;
}
//if (ms2.Substring(0, 5) == "notify")
//{
// ms2 = ms2.Substring(0, 6);
//}
Form1 fmg = new Form1();
fmg.notifyIcon1.ShowBalloonTip(5000, "Attention", ms2, ToolTipIcon.Info);
//MessageBox.Show(ms2); //the message that the client sent back
ASCIIEncoding asen = new ASCIIEncoding();
//s.Send(asen.GetBytes("The string was recieved by the server.")); //sending a acknowledgement message to the client
// MessageBox.Show("\\nSent Acknowledgement");
s.Close();
myList.Stop();


// }
//catch (Exception e)
//{
// MessageBox.Show("Error..... " + e.StackTrace);
}
// }

public void Client(String notifymessage)
{
try
{
TcpClient tcpclnt = new TcpClient();
//MessageBox.Show("Connecting.....");
//the ipadress you want to send the message to
//needs to be stored in a db, loop to send to all users
tcpclnt.Connect("", 8001); // use the ipaddress as in the server program
// MessageBox.Show("Connected");
//MessageBox.Show("Enter the string to be transmitted : ");
// String str = Console.ReadLine();
String str = notifymessage;
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
//MessageBox.Show("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
char ms;
string ms2 = "";
for (int i = 0; i < k; i++)
{
ms = (Convert.ToChar(bb[i]));
ms2 = ms2 + ms;
}

//MessageBox.Show(ms2);
tcpclnt.Close();

}
catch (Exception e)
{
MessageBox.Show("Error..... " + e.StackTrace);
}
}





Thanks

Lee

您可以看看使用WCF提取一些底层套接字和TCP编码.这样,它就处于配置状态,您可以在应用程序本身上进行更多讨论.

Sasha在使用WCF聊天应用程序方面写了一篇不错的文章.它可能会帮助您了解WCF可以为您做什么.

WCF/WPF聊天应用程序 [
You could look at using WCF to abstract some of the low level socket and TCP coding away. This way it is just in a configuration and you can forcus more on the app itself.

Sasha did a good article on using WCF for a chat app. It might help you to see what WCF can do for you.

WCF / WPF Chat Application[^]