Eclipse SDK/Socket Programming help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 27
Reputation: winky is an unknown quantity at this point 
Solved Threads: 0
winky's Avatar
winky winky is offline Offline
Light Poster

Eclipse SDK/Socket Programming help

 
0
  #1
Feb 12th, 2008
Hey everybody,

My current Networking teacher gave us sample code to show us how socket programming works. Problem is, I haven't had to use JAVA in so forever, let alone ECLIPSE (although I hear it's pretty nice). So, I was wondering how to get the program to work. I have it written in the classes and the classes put into the project, but then I'm not sure whether I should be using the "Run As.." Java Application for the entire the project.... the server first..... or what. Any insight would be awesome.

Here is what I'm looking at.
Server:
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class Server2 {
  5.  
  6. public static void main(String args[]) throws Exception
  7. {
  8.  
  9. DatagramSocket serverSocket = new DatagramSocket(9876);
  10.  
  11. byte[] receiveData = new byte[1024];
  12. byte[] sendData = new byte[1024];
  13.  
  14. while(true)
  15. {
  16.  
  17. DatagramPacket receivePacket =
  18. new DatagramPacket(receiveData, receiveData.length);
  19. serverSocket.receive(receivePacket);
  20. String sentence = new String(receivePacket.getData());
  21.  
  22. InetAddress IPAddress = receivePacket.getAddress();
  23.  
  24. int port = receivePacket.getPort();
  25.  
  26. String capitalizedSentence = sentence.toUpperCase();
  27.  
  28. sendData = capitalizedSentence.getBytes();
  29.  
  30. DatagramPacket sendPacket =
  31. new DatagramPacket(sendData, sendData.length, IPAddress,
  32. port);
  33.  
  34. serverSocket.send(sendPacket);
  35. }
  36. }
  37. }

Client:
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class Client2 {
  5.  
  6. public static void main(String args[]) throws Exception
  7. {
  8.  
  9. BufferedReader inFromUser =
  10. new BufferedReader(new InputStreamReader(System.in));
  11.  
  12. DatagramSocket clientSocket = new DatagramSocket();
  13.  
  14. InetAddress IPAddress = InetAddress.getByName("hostname");
  15.  
  16. byte[] sendData = new byte[1024];
  17. byte[] receiveData = new byte[1024];
  18.  
  19. String sentence = inFromUser.readLine();
  20. sendData = sentence.getBytes();
  21. DatagramPacket sendPacket =
  22. new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
  23.  
  24. clientSocket.send(sendPacket);
  25.  
  26. DatagramPacket receivePacket =
  27. new DatagramPacket(receiveData, receiveData.length);
  28.  
  29. clientSocket.receive(receivePacket);
  30.  
  31. String modifiedSentence =
  32. new String(receivePacket.getData());
  33.  
  34. System.out.println("FROM SERVER:" + modifiedSentence);
  35. clientSocket.close();
  36. }
  37. }

Thanks for any help guys
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Eclipse SDK/Socket Programming help

 
0
  #2
Feb 13th, 2008
run both as separate project and separate program.
but run server before running client.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2037 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC