curlyfries1999's Profile
Reputation: 0
Apprentice
- Group:
- Active Members
- Active Posts:
- 111 (0.21 per day)
- Joined:
- 22-December 11
- Profile Views:
- 530
- Last Active:
May 09 2013 10:31 AM- Currently:
- Offline
Previous Fields
- Country:
- GB
- OS Preference:
- Windows
- Favorite Browser:
- FireFox
- Favorite Processor:
- Intel
- Favorite Gaming Platform:
- Playstation
- Your Car:
- Who Cares
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Game developer help incitment
Posted 9 May 2013
I would just like to say, the main thing we want somebody to do is clean up the code. I think we have written it well so far but I would like to see what somebody else thinks.
Thanks, Curly
-
In Topic: Send message to all connected clients
Posted 2 Mar 2013
RJCoder8989, on 02 March 2013 - 07:41 AM, said:
RJCoder8989, on 02 March 2013 - 12:33 AM, said:
curlyfries1999, on 17 February 2013 - 12:04 PM, said:import java.io.*; import java.net.*; public class ServerChat implements Runnable { static ServerSocket server; static Socket client; static class Read implements Runnable { public void run() { try { InputStream istream=client.getInputStream(); InputStreamReader isreader=new InputStreamReader(istream); BufferedReader breader=new BufferedReader(isreader); String reader; while((reader=breader.readLine())!=null) { System.out.println("From Client:"+reader); } breader.close(); istream.close(); isreader.close(); client.close(); } catch(Exception read) { System.out.println(read); } } } static class Write implements Runnable { public void run() { try { OutputStream opstream=client.getOutputStream(); PrintStream pstream=new PrintStream(opstream); BufferedReader breader1=new BufferedReader(new InputStreamReader(System.in)); String write; while(!(write=breader1.readLine()).equals("exit")) { pstream.println(write); } breader1.close(); opstream.close(); pstream.close(); client.close(); } catch(Exception write) { System.out.println(write); } } } int n=1; public void run() { for(;;)/>/>/> { try { Read read=new Read(); Write write=new Write(); client=server.accept(); System.out.println("Connection established from client:"+n); Thread t1=new Thread(read); Thread t2=new Thread(write); t1.start(); t2.start(); } catch(Exception e) { System.out.println(e); } n++; } } public static void main(String[] args)throws Exception { ServerChat chat=new ServerChat(); server=new ServerSocket(6666); System.out.println("Waiting for connection"); Thread clientthread=new Thread(chat); Thread clientthread1=new Thread(chat); clientthread.start(); clientthread1.start(); } }
Download my project from GitHub: https://github.com/c...ab=repositories
I think I have fixed the problem. The client is the repository named Game. -
In Topic: Send message to all connected clients
Posted 20 Feb 2013
pbl, on 20 February 2013 - 04:59 PM, said:You don't need a thread for the send... there is no reason fro the send to while(true)
it can be done in the main thread
The Client starts a thread to red the messages from the Server
when a message is received, it calls a method in the main thread to process it
When the user invoke something in the Client gui a message is send to the Server
class Client extends JFrame implements ActionListener { Client() { ...connect to server get socket ...set up GUI add(jtextField); ClientThread ct = new ClientThread(this, socket); ct.start(); public void actionPerformed(ActionEvent e) { // a enter in the JTextField sends its text tro the Server printWriter.write(textfield.getText()); } void fromServer(String msg) { ... process message from server } class ClientThread extends Thread { Client client; ClientThread(Client client, Socket socket) { .. save client (my father) ... build inoput stream } public void run() { while(true) { ... String msg = receive from server client.fromServer(msg); }
Have fun
P.S.
if it is for a agame better to use ObjectInputStream and ObjectOutputStream will simplify your life a lot and you can switch() on the message type
So what's the benifit of using ObjectInputStream and ObjectOutputStream? Does it increase performance? -
In Topic: Send message to all connected clients
Posted 20 Feb 2013
I have updated my Client program and it seems to work nicely. I don't know how efficient or reliable it is but here is the code:
Client class:
import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { Socket socket = null; static ClientThreadReceive ctr = null; static ClientThreadSend cts = null; String name = null; String[] IPParse; String IP; int port; public static void main(String[] args) throws IOException { new Client().runClient(args[0], args[1]); } public void runClient(String name, String IPPort) throws UnknownHostException, IOException { IPParse = IPPort.split(":"); port = Integer.parseInt(IPParse[1]); IP = IPParse[0]; Socket socket = new Socket(IP, port); PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); printWriter.println(name); ctr = new ClientThreadReceive(socket, name); ctr.start(); cts = new ClientThreadSend(socket, name); cts.start(); } }
ClientThreadReceive class:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; public class ClientThreadReceive extends Thread { private Socket socket; private String name; public ClientThreadReceive(Socket socket, String name) { this.socket = socket; this.name = name; } public void run() { try { BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) { if (!input.readLine().startsWith(name)) { System.out.println(input.readLine()); } } } catch (IOException e) { e.printStackTrace(); } } }
ClientThreadSend class:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class ClientThreadSend extends Thread { private Socket socket; String name; public ClientThreadSend(Socket socket, String name) { this.socket = socket; this.name = name; } public void run() { try { BufferedReader bufferedReaderFromCommandPrompt = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); while (true) { String readerInput = bufferedReaderFromCommandPrompt.readLine(); printWriter.println(name + ": " + readerInput); } } catch (IOException e) { e.printStackTrace(); } } } -
In Topic: Send message to all connected clients
Posted 19 Feb 2013
pbl, on 19 February 2013 - 08:36 PM, said:So you have your answer.
You need a class to define what your Screen looks like (with all the squares coordinates and color)
This class implements Serializable so objects of that class can be sent over the wire.
Asynchronous Client receive MouseClick and or CursorKey Move from the user and send them to the Server
The Server updates what the screen should look and broadcast that Screen Definition object to all Client
Clients when receiving that object call their repaint() method which calls paint()
paint() draw the screen based on the definition on that screen definition object
Thanks a lot. This really helps
My Information
- Member Title:
- D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
-
- Years Programming:
- 1
- Programming Languages:
- VB.NET, C#
Contact Information
- E-mail:
- Click here to e-mail me
Friends
curlyfries1999 hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
curlyfries1999 has no profile comments yet. Why not say hello?