|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: May 2011
Messaggi: 1
|
Gioco problema connessione
Ciau a tutti sono Manuel, sono nuovo, ma seguo da molto tempo il vostro splendido forum!
Volevo chiedervi un aiutino per un programma, che dobbiamo fare a scuola. Il programma in questione è un gioco, che consiste in premere 100 volte il mouse nel minor tempo possibile. Il programma è pensato così che al 100-esimo click il Client apre una DataOutputStream e manda tramite il metodo writeInt la variabile Time2 (che è differenza di tempo tra quando premiamo il tasto start ed il tempo al 100-esimo click). Il server tramite DataInputStream e metodo readInt, riceve il tempo dal Client, lo salva nella tabella Time e fa il sorting dei tempi per dopo stamparli sullo schermo. Il problema? Che non mi manda i tempi al server, perchè mi da socket closed. Client: Codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import sun.audio.*;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.*;
import java.net.*;
public class SpeedMouseClient extends JFrame implements Runnable{
ImageIcon img;
int count=1;
long Time, time1, time2;
JFrame f = new JFrame();
JTextField field = new JTextField(10);
JTextField field2 = new JTextField(10);
JLabel label = new JLabel(" START");
JLabel label_Gumb = new JLabel (" CLICK ME!!!");
JLabel imageLabel = new JLabel();
public SpeedMouseClient() {
initComponents();
sound();
}
//Zgradi okno
private void initComponents() {
img = new ImageIcon("images/1.png");
final JButton Gumb = new JButton(img);
Gumb.setPreferredSize(new Dimension(258,268));
Gumb.add(label_Gumb);
label_Gumb.setForeground(Color.white);
label_Gumb.setFont(new Font("sansserif",Font.BOLD,30));
final JButton Start = new JButton();
Start.setPreferredSize(new Dimension(80,80));
img = new ImageIcon("images/1.png");
Start.setIcon(img);
label.setForeground(Color.white);
Start.add(label);
imageLabel.setForeground(Color.white);
Start.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent d){
Calendar clock1 = Calendar.getInstance();
time1=clock1.getTimeInMillis();
sound2();
}//mouseClicked
});
Gumb.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
field.setText(" Clicks: " + count);
count++;
if(count==21){
Calendar clock2 = Calendar.getInstance();
time2=clock2.getTimeInMillis();
Time=time2-time1;
field2.setText(" Time: "+ Time);
int Time2 = (int)Time;
DataOutputStream dos;
try{
dos = new DataOutputStream(clientSocket.getOutputStream());
dos.writeInt(Time2);
}catch(IOException err) {
err.printStackTrace();
}
}
}//mouseClicked
});
try {
f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("images/bg.jpg")))));
}catch(IOException e) {
e.printStackTrace();
}
f.pack();
Image icon = Toolkit.getDefaultToolkit().getImage("images/icon.png");
f.setVisible(true);
f.setSize(500,500);
f.setResizable(false);
f.setIconImage(icon);
field.setEditable(false);
field.setFont(new Font("sansserif",Font.BOLD,14));
field2.setEditable(false);
field2.setFont(new Font("sansserif",Font.BOLD,14));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
c.gridx = 1;
c.gridy = 0;
f.add(Gumb, c);
c.anchor = GridBagConstraints.LAST_LINE_START;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 1;
f.add(Start, c);
c.anchor = GridBagConstraints.PAGE_END;
c.gridx = 1;
c.gridy = 1;
f.add(field, c);
c.anchor = GridBagConstraints.LAST_LINE_END;
c.gridx = 2;
c.gridy = 1;
f.add(field2, c);
}//initComponents
public void sound(){
try{
String gongFile = "sound.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
}catch(FileNotFoundException fnfe){}
catch(IOException ioe){}
}//sound
public void sound2(){
try{
String gongFile = "start.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
}catch(FileNotFoundException fnfe){}
catch(IOException ioe){}
}//sound
static Socket clientSocket = null;
static DataOutputStream dos = null;
static DataInputStream is = null;
static BufferedReader inputLine = null;
static boolean closed = false;
public static void main(String[] args) {
SpeedMouseClient spd = new SpeedMouseClient();
try {
clientSocket = new Socket("localhost",2046);
dos = new DataOutputStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host ");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the host ");
}
if (clientSocket != null && dos != null && is != null) {
try {
dos.close();
is.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}//if
}//main
public void run() {
String responseLine;
try{
while ((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("*** Bye") != -1) break;
}
closed=true;
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}//run
}//SpeedMouseClient
Server: Codice:
import java.io.*;
import java.net.*;
import java.util.Arrays;
public class SpeedMouseServer{
public static void main(String args[]){
int n=2;//numero di giocatori
Socket clientSocket[]=new Socket[n];
ServerSocket serverSocket=null;
DataInputStream dis=null;
DataOutputStream dos=null;
String vrstica;
int Time[] = new int[n];
try{
serverSocket = new ServerSocket(2046);
}catch(IOException e)
{}
try{
for(int i=0; i<n; i++){
clientSocket[i]= serverSocket.accept();
}
}catch(IOException e){}
try{
for(int i=0; i<n; i++){
dis = new DataInputStream(clientSocket[i].getInputStream());
Time[i]=dis.readInt();
}//for
Arrays.sort(Time);
for(int i=0; i<n; i++){
System.out.println("Cas: "+Time[i]+"ms");
}//for
//dos = new DataOutputStream(clientSocket[i].getOutputStream());
for(int i=0; i<n; i++){
clientSocket[i].close();
}
}catch(IOException e){};
}//main
}//SpeedMouseServer
Scusate lo stile di programmazione ![]() Vi allego i files: http://www.megaupload.com/?d=NX30S9DV Ultima modifica di manu51 : 30-05-2011 alle 17:19. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 05:58.




















