Improve company productivity with a Business Account.Sign Up

x
?
Solved

Java networking

Posted on 2002-07-07
10
Medium Priority
?
219 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
9 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 160 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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 

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 36

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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

595 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