473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with connecting udp socket

Hi,

I'm writing a simple udp client using sockets. My code looks like
this:

initServer2 = new Socket(AddressF amily.InterNetw ork, SocketType.Dgra m,
ProtocolType.Ud p);
initServer2.Con nect(IPAddress. Parse(Propertie s.Settings.Defa ult.InitServerA ddress),
Properties.Sett ings.Default.In itServerPort);

initServer2.Sen d(Encoding.ASCI I.GetBytes("REG ISTER2PORT-" +
userName));
initServer2.Rec eive(buffer);

initServer2.Con nect(IPAddress. Any, 0);
initServer2.Rec eive(buffer);

First I connect my socket to a specified ip and port, then I send and
receive data. next thing I want to do is opening the same socket that
it can accept every incoming package.

Everything works fine under Windows Vista, but when I run it under
Windows Xp I get an SocketException from line:
initServer2.Con nect(IPAddress. Any, 0); that the requeste address is
not valid in its context: 0.0.0.0:0.

Someone knows what is wrong with that code?
Thanks for any help
Dec 21 '07 #1
1 3622
On Fri, 21 Dec 2007 05:28:54 -0800, El*****@gmail.c om <El*****@gmail. com>
wrote:
[...]
initServer2.Con nect(IPAddress. Any, 0);
initServer2.Rec eive(buffer);

First I connect my socket to a specified ip and port, then I send and
receive data. next thing I want to do is opening the same socket that
it can accept every incoming package.

Everything works fine under Windows Vista, but when I run it under
Windows Xp I get an SocketException from line:
initServer2.Con nect(IPAddress. Any, 0); that the requeste address is
not valid in its context: 0.0.0.0:0.

Someone knows what is wrong with that code?
Well, I can't explain why Vista works. I don't have enough experience
with that OS to comment. I suspect that Vista is simply delaying the
error; if you tried to send using the Send() method after that statement,
I suspect you'd still get an error.

But as for the general issue, the basic problem is that "connecting " a UDP
socket doesn't really do what you seem to think it does anyway.

A UDP socket is a connectionless socket. You can call Connect(), but all
that does is define a default remote endpoint for the communications.
That is, the destination to which data sent from the socket will go, if
you don't provide an address when you send (i.e. using SendTo()). It's
not useful for receiving at all. Any sender with your UDP socket's
address and port will be able to send you data.

In addition, connecting to IPAddress.Any doesn't really make any sense.
You can't send data to IPAddress.Any. If you are trying to send or
receive broadcast data, you need to use the broadcast address
255.255.255.255 and set the Broadcast socket option. If you are trying to
send to a specific endpoint, you need to use a valid and correct endpoint
address.

You should probably visit the Winsock FAQ at
http://tangentsoft.net/wskfaq/ It has a lot of good advice for people new
to writing network code, especially those using sockets on Windows.

Pete
Dec 21 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2537
by: Ali | last post by:
I'm trying to write a really basic chat program that allows 2 client programs to send messages to each other via a server. I've managed to write the code so that both clients can connect to the server and send and recieve messages to/from the server but when I send a message from one client I can't get the message to appear in the second client. I think it's something to do with threads but I don't know what! I've been looking at the...
2
1398
by: Nick Zdunic | last post by:
Ok - something for socket jockeys I have a .NET application using the TcpClient class from System.Net.Sockets I use this to connect to an IP address/Socket on the other side of Australia. There is a problem though. The read from the socket returns 0 bytes which means the socket/host has closed down.
3
5129
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library. When i try to connect to a server that doesnt exist it raises a "Connection forcibly rejected by the resmote host" SocketException.
0
1912
by: richard | last post by:
OS: Winxp and Win2003 Visual Basic.NET 2003 MS-SQL Server 2000 hey all I am a newbie in vb.net but i have managed to build a simple chat server in vb.net using socket and a client connecting to it made in vb6. Some of the code in the server are based on the basic samples that i get from other authors. To give you an idea, the server uses sockets,
0
1459
by: Usman | last post by:
Hi I'm having problem with a scenarion where I have a server written in C# and client written in VC6++. Here is the server code that i'm using including the Callback function for handling clients. Also there's a commented code where i'm using TCPListener instead of simple Socket Class. The problem i'm having is that when I run this code using Socket class, the server starts well but "OnClientConnect" method never gets called. On...
2
5785
by: Daniel Lindberg | last post by:
Hi, im a beginner in C# and i just cant seem to get my udp broadcasting to work. What happens is that it just locks down on the Receive call and never gets any data. I have the code for the server who answers the broadcast, so i know that it sends a reply, but i cant figure out why my C# client cant receive it.. Any input on whats wrong with my code will be appreciated //My code...
1
1730
by: Rain | last post by:
HElp! Im new heres my code: public class SocketExcept { public static void Main() { IPAddress host = IPAddress.Parse("192.168.1.1"); IPEndPoint hostep = new IPEndPoint(host, 8000); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
9
4300
by: darthghandi | last post by:
I am trying to create a server application using asynchronous sockets. I run into a problem when I try to connect to my server using a non-.net program. I can establish the connection, and send some packets in response to the programs first message, but when I receive a response back, it's the last packet I sent. After that packet, I receive the packet I really wanted. This only happens when I connect with local host. I tried...
8
2180
by: JDavis | last post by:
I am using System.Net.Sockets to connect a client socket to a server that requires three inputs when I connect: host, port and an identification number that identifies the person connecting. None of the overloads for either the Connect or BeginConnect methods takes that many inputs (basically they take host and port only). Would it be best to create a new class that derives from the base class System.Net.Sockets and implement a new...
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8899
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
8871
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
7738
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...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
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
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.