473,378 Members | 1,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 15535
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

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

Similar topics

3
by: ^CeFoS^ | last post by:
Hi to everybody, due to I want to use the serial port of a server machine through an applet allocated in html document. > Then one application will run in the server machine and using > the serial...
2
by: quotto | last post by:
I have written a web server application using ASP.Net that handles requests from an HTTP client. I wrote a small test client in VB6 using the MSINet ActiveX control, to load test the server. The...
0
by: Maciek | last post by:
Hi When I set Session state mode to StateServer (IIS 6.0; windows2003; .NET 2.0) in my application, I have recived this message:...
8
by: Woody Splawn | last post by:
I am asking this question here because I asked this question in the Reporting Services Newsgroup and did not get an answer. Does anyone know if Reporting Services is intended to work in a...
4
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
14
by: Developer | last post by:
Hello All, i have recently installed VS2005 and was trying to install SQL sever 2000. I have Win XP' SP2. But when I tried installing, it only installed client tools and not the database. Can...
3
by: Mark Delaney | last post by:
When using the MS SQL 2005 JDBC driver, I now need to have the DNS name resolution to the client correctly set up. If not I get the following error: SQLState: 08S01 SQLError: 0 Message:...
7
by: David | last post by:
i think i just realized i'm an idiot. again. (not syntactically correct code... just pieces to illustrate) class StateObject { members like socket, receiveBuffer, receiveBufferSize,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.