| | |
Eclipse SDK/Socket Programming help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
Client:
Thanks for any help guys
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:
Java Syntax (Toggle Plain Text)
import java.io.*; import java.net.*; public class Server2 { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } }
Client:
Java Syntax (Toggle Plain Text)
import java.io.*; import java.net.*; public class Client2 { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } }
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
-George Carrette
run both as separate project and separate program.
but run server before running client.
but run server before running client.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Indian Developer
http://falaque.wordpress.com/
![]() |
Other Threads in the Java Forum
- Previous Thread: retreiving info from oracle db fields into textboxes
- Next Thread: how to create and load shared libraries in fedora 7?
Views: 2037 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time tree webservices windows





