473,725 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# UDP server client on same machine

2 New Member
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 15573
Plater
7,872 Recognized Expert Expert
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
desire83
2 New Member
thanks for ur help...but since i said im new in programming...c an 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 Recognized Expert Expert
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
5344
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 port. The other application that will be called in the html > document that it'll allocate the frame with the user interfaz and > it'll manage the events (applet). > > But I have any questions. I suppose the server application will be > running...
2
1748
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 client makes repeated requests of the server and keeps track of the number per second it is able to get processed. When I run both the client and server on one machine, I get about 150 per second. When I move the client to another machine on my LAN, the...
0
800
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: ----------------------------------------------------------------------------------------------------------------- Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same. If the server is on a...
8
2747
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 client/Server or Local machine environment? Based on what I have seen my guess is yes but that is a guess. In some materal it talks about it running on a web server but my supposition is that this does not necessarily mean that it would not make for a...
4
6734
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
6964
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 attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
14
3033
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 anyone please help me with this as I want to install SQL server and also wouold be grateful, if you can suggest me any workaround to dealwith this problem.(Like should I install any new OS etc). Any help would be appreciated.
3
7225
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: hostname : hostname Where "hostname" is the client host name. It doesn't seem to matter if I
7
1957
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, StringBuilder etc.. }
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.