Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: socket Network TCP/IP
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
Posted 2-Feb-11 7:58am
leemalin1.9K
Edited 3-Feb-11 7:48am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 209
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 3 Feb 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100