Solved

Java networking

Posted on 2002-07-07
10
169 Views
Last Modified: 2010-03-31
Hi All
Can I open TCP or UDP connection to a server and identify my self as other IP address? I want to write a simple unit test for my application that need to send packets from some resources, can I do it in java? is there anyt way doing it?
   Thanks
0
Comment
Question by:dannysh
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 2

Expert Comment

by:nir2002
ID: 7134810
Hi Danny,

Although I have not try, for both TCP,UDP have constructors
that get the local address:

UDP
===
public DatagramSocket(int port,
                      InetAddress laddr)
               throws SocketException

Creates a datagram socket, bound to the specified local address. The local port must be between 0 and 65535 inclusive.

TCP
===
public Socket(InetAddress address,
              int port,
              InetAddress localAddr,
              int localPort)
       throws IOException
Creates a socket and connects it to the specified remote address on the specified remote port. The Socket will also bind() to the local address and port supplied.

Although I am not certain that the TCP will work as you expected since it reliable protocol and the communication need feedback.

Best regards
Nir
0
 
LVL 3

Accepted Solution

by:
yasser_helmy earned 40 total points
ID: 7134816
No. The ip addr. is automatically written in the socket. You cant modify it.
But if you want to test the server and client on the same machine, you can use two different ports. For example, the server uses port 8000 and the client uses port 9000.
0
 
LVL 2

Expert Comment

by:nir2002
ID: 7134819
so why there is an option for local address?
0
 

Author Comment

by:dannysh
ID: 7134845
I think Nir is right in his question. I will test your solution.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 86

Expert Comment

by:CEHJ
ID: 7135296
>>Creates a socket and connects it to the specified remote address on the specified remote port. The Socket will also bind() to the local address and port supplied.

Only if you own that address.

What you're trying to do is spoof your IP address. This could only be done with JNI. It requires low-level access to do this. ASFAIK, in Windows, low-level would mean *below* the Win API.
0
 

Author Comment

by:dannysh
ID: 7135306
Hi CEHJ
So what is the meaning of local address in the socket constructor?
0
 
LVL 92

Expert Comment

by:objects
ID: 7136379
Its used in cases where your box has more than one IP addesses, such as if it has more than one network card.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7136387
>>Its used in cases where your box has more than one IP addesses, such as if it has more than one network card.

Strange - I've posted exactly the same thing - but it didn't appear!?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8894125
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to yasser_helmy

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now