Results 21 to 26 of 26
- 01-10-2011, 08:38 AM #21
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
here's an example of a client /server
Java Code:import java.io.*; import java.net.*; public class Client { private static Socket client; private static ObjectInputStream input; private static ObjectOutputStream output; public static void main(String[] args) throws IOException, EOFException{ System.out.println("connecting to server.."); client = new Socket(InetAddress.getByName("127.0.0.1"), 12345); System.out.println("connected to : " + client.getInetAddress().getHostName() ); output = new ObjectOutputStream( client.getOutputStream() ); output.flush(); output.writeObject( args[0] ); output.flush(); output.close(); client.close(); } }Java Code:import java.net.*; import java.io.*; public class Server { private static ObjectInputStream input; private static ObjectOutputStream output; private static ServerSocket server; private static Socket connection; public static void main(String[] args) throws Exception{ server = new ServerSocket(12345,100); while (true){ System.out.println("waiting for connection.."); connection = server.accept(); System.out.println("Connection received: " + connection.getInetAddress().getHostName()); output = new ObjectOutputStream( connection.getOutputStream() ); output.flush(); input = new ObjectInputStream(connection.getInputStream() ); System.out.println("Recv'd stream"); String message = (String) input.readObject() ; System.out.println(message); output.close(); input.close(); connection.close(); } } }
- 01-10-2011, 08:58 AM #22
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-10-2011, 08:58 AM #23
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
thanks for everyone.
- 01-10-2011, 03:57 PM #24
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
Hi Krish - its advisable that you read some client server tutorials just as Fubarable mentioned in the earlier post. JosAH's code base is also a good starting point.
I learned socket programming using the Java tutorials, so here is a link to them for your reference: Reading from and Writing to a Socket (The Java Tutorials > Custom Networking > All About Sockets)
Have a thorough read through and see if it helps in your solution. If not, post back here, lets try to debug your code.
Best,--user0--
- 01-10-2011, 10:36 PM #25
Member
- Join Date
- Jan 2011
- Posts
- 26
- Rep Power
- 0
Simple Server & Client
Server:
Client:Java Code:import java.net.*; import java.io.*; public class SimpleServer { public static void main(String args[]) { ServerSocket s = null; Socket s1; String sendString ="Hello network world!!!!"; OutputStream os; DataOutputStream dos; try { s = new ServerSocket(5432); } catch (IOException e) { } while (true) { try { // wait here and listen for a connection s1 = s.accept(); //Get a communication stream associated with the socket os = s1.getOutputStream(); dos = new DataOutputStream (os); //Send your string! (UTF provides machine independence) dos.writeUTF(sendString); // close the connection, but not the server socket dos.close(); os.close(); s1.close(); } catch (IOException e) { // ignore } } } }
Hope that helps as a template.Java Code:import java.net.*; import java.io.*; public class SimpleClient { public static void main(String args[]) throws IOException { Socket s1; InputStream s1In; DataInputStream dis; s1 = new Socket("127.0.0.1", 5432); s1In = s1.getInputStream(); dis = new DataInputStream(s1In); String st = new String(dis.readUTF( )); System.out.println(st); dis.close(); s1In.close(); s1.close(); } }
- 01-11-2011, 02:13 AM #26
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Java Server/C Client
By FallenBlade in forum NetworkingReplies: 13Last Post: 03-10-2011, 11:22 PM -
server-client; client sends a username to the server.
By lkcz in forum New To JavaReplies: 2Last Post: 09-24-2010, 11:31 AM -
java server and c client ?????????
By biebo in forum NetworkingReplies: 7Last Post: 07-24-2010, 04:35 AM -
Java Message Server/Client help
By sari in forum NetworkingReplies: 3Last Post: 03-20-2010, 11:17 PM -
Creating an IRC client in Java
By VeasMKII in forum New To JavaReplies: 0Last Post: 06-17-2009, 09:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks