Solved

Java networking

Posted on 2002-07-07
10
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:dannysh
ID: 7134845
I think Nir is right in his question. I will test your solution.
0
 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

622 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