471,604 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,604 software developers and data experts.

C# UDP server client on same machine

hello, im new in c#.net...
i have made a server application and a client application for the same machine...the server is suppos to send a string to client when the button on the server application is pressed...but im not recieving the string on my client application
here are the codes for both the applications
SERVER:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.IO;
  12.  
  13. namespace SOCKETexp2
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         //Declaration of network variables/////
  23.  
  24.         Socket sock_server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  25.         static int port_server = 5000;
  26.         static string name_server = "mine";
  27.         static int port_client = 5001;
  28.         IPEndPoint serverEP = new IPEndPoint(Dns.GetHostEntry(name_server).AddressList[0], port_server);
  29.         IPEndPoint clientEP = new IPEndPoint(Dns.GetHostEntry(name_server).AddressList[0], port_client);
  30.  
  31.  
  32.  
  33.         private void button1_Click(object sender, EventArgs e)
  34.         {
  35.             string enter_string = tb_string.Text;
  36.             byte[] send_byte;
  37.             send_byte = System.Text.Encoding.Unicode.GetBytes(enter_string);
  38.             sock_server.Bind(serverEP);
  39.             sock_server.SendTo(send_byte, SocketFlags.None, clientEP);
  40.         }
  41.  
NOW THE CLIENT APPLICATION:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12. namespace socketexp22
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         //Declaration of network variables/////
  22.  
  23.         Socket sock_server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  24.         static int port_server = 5000;
  25.         static string name_server = "mine";
  26.         static int port_client = 5001;
  27.         IPEndPoint serverEP = new IPEndPoint(Dns.GetHostEntry(name_server).AddressList[0], port_server);
  28.         IPEndPoint clientEP = new IPEndPoint(Dns.GetHostEntry(name_server).AddressList[0], port_client);
  29.  
  30.         private void button1_Click(object sender, EventArgs e)
  31.         {
  32.             EndPoint Remote = (EndPoint)serverEP;
  33.             sock_server.Bind(clientEP);
  34.             byte[] byte_recieve=new byte[1024];
  35.             sock_server.ReceiveFrom(byte_recieve, SocketFlags.None, ref Remote);
  36.             string server_text = System.Text.Encoding.Unicode.GetString(byte_recieve);
  37.             tb_string.Text = server_text;
  38.  
  39.         }
  40.     }
  41. }
  42.  
Dec 2 '08 #1
3 15303
Plater
7,872 Expert 4TB
I would try making them not specific to listening to each other.
start by telling them to listen for all udp traffic on the ports you specified.
I would imagine that since both programs are running on the same computer, they will use the localhost ip address, and not the one from the DNS calls, so you are not listening for the correct address.
Listening to all addresses on that port should let you see the message and you can find out who it is coming from
Dec 2 '08 #2
thanks for ur help...but since i said im new in programming...can u guide me how to listen to all ports like u said....and is the local IP add always 127.0.0.1?
Dec 3 '08 #3
Plater
7,872 Expert 4TB
Use IPAddress.Any instead of an actual IP address
Dec 3 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Woody Splawn | last post: by
14 posts views Thread by Developer | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.