Voilà, nous sommes sur un projet de 4 mois de programmation d'un jeu.

Je tente de faire circuler des objets entre client-server. J'obtiens cette erreur à chaque ouverture des streams .
Voici le code , "très" petites parties de tout le système (si besoin demander d'autres portions)

Cette classe gère toute les connexions
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
 
package Model;
 
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
import javax.swing.JFrame;
import javax.swing.JOptionPane;
 
/* Conserve les sockets, isoles les erreurs réseaux et gère les fermeture
 * isole responsabilité et améliore visibilité :) :)
 * 
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! fermer proprement meme en cas derreur
 * 
 * */
public class NetConfig 
{
 
	private ServerSocket serverSocket;
 
	private Socket socket;
 
	private boolean isClient;
	private ObjectInputStream in;
	private ObjectOutputStream out;
 
 
	/*Pas de construct, init via setters, car sert à fermer proprement les connections en cas d'exit*/
 
	public void openStreams()
	{		
			System.out.println("Je suis client: "+isClient());
			try 
			{
				out = new ObjectOutputStream (socket.getOutputStream());
				in   = new ObjectInputStream (socket.getInputStream());
 
			} 
			catch (IOException e) 
			{
				netBug("Ouverture stream de"+socket);
				closeStreams();
				closeSockets();
				netBug("Fermeture des streams & sockets puis exit");
				e.printStackTrace();
				System.exit(-1);
			}		
 
	}
 
	/*
	 * Gestionnaire d'erreurs réseaux
	 */
	public static void netBug(String string) 
	{
		JFrame frame = new JFrame("Message Dialog");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JOptionPane.showMessageDialog(frame, "Erreur Réseau :"+string, "Attention",
				JOptionPane.WARNING_MESSAGE);
		return;
	}
 
	public synchronized void writeObj(Object obj)
	{
		try 
		{
			getOut().writeObject(obj);
			getOut().flush();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
	public synchronized Object readObj() 
	{
		Object in = null;
		try 
		{
			in = getIn().readObject();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return in;
 
 
	}
 
 
 
	public void closeStreams()
	{
		try 
		{
			if(in!= null && out!=null)
			{
					in.close();
					out.close();
			} 
		}
		catch (IOException e) 
		{
			netBug("Erreur fermuture");
			e.printStackTrace();
		}
 
	}
 
	/* @ pre : objet init par l'user */
	public void closeSockets()
	{
		/* protection si exit précoce du jeu cf. Kicker*/
		if(socket==null){return;}
 
		try
			{
				socket.close();
			} 
 
		catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
 
		/* protection si exit précoce du jeu cf. Kicker*/
		if(serverSocket==null){return;}
 
		if(isServer())
		{			
			try 
			{
				serverSocket.close();
			} 
			catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
 
 
 
	}
 
	/*------------------------------------------------------*/
	public ServerSocket getServerSocket() {
		return serverSocket;
	}
 
	public void setServerSocket(ServerSocket serverSocket) {
		this.serverSocket = serverSocket;
	}
 
 
 
	public void setSocket(Socket socket) {
		this.socket = socket;
	}
 
 
	public Socket getSocket() {
		return socket;
	}
 
	/*----------------------------------*/
 
	public boolean isClient()
	{
		return this.isClient;
	}
 
	public void setClient(boolean flag)
	{
		this.isClient=flag;
	}
 
	public boolean isServer()
	{
		return !this.isClient;
	}
 
	/*-----------------------------------*/
 
	public ObjectInputStream getIn() {
		return in;
	}
 
	public synchronized void setIn(ObjectInputStream in) {
		this.in = in;
	}
 
	public ObjectOutputStream getOut() {
		return out;
	}
 
	public synchronized void setOut(ObjectOutputStream out) {
		this.out = out;
	}
 
 
 
}
Celle-ci utilise le réseau
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
		 * Crée et attend réponse réseau
		 */
		private class Connecter implements Runnable,GameConstants
		{
 
			private NetConfig netconfig;
 
			public void run()
			{
				/* ouverture serveur*/
				try 
				{
					serverSocket = new ServerSocket(PORT);
					log.write("run de connecter de netdec ouverture",serverSocket+"nullle");
				} 
				catch (IOException e1) 
				{
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
 
				/* ouverture socket*/
				try 
				{
					socket = serverSocket.accept();
					log.write("socket ouvert server",serverSocket+"  "+socket);
				} 
				catch (IOException e1) 
				{
					NetConfig.netBug("Iciiiiiiiiiii");
					log.write("BUGGG",serverSocket+" // " + socket);
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}				
 
				/* init de l'interface réseau*/
				log.write("init",serverSocket+" // " + socket);/* plus rien ici*/
				netconfig.setClient(false);
				netconfig.setSocket(socket);
				netconfig.setServerSocket(serverSocket);
				log.write("run de connecter de netdec",serverSocket+" // " + socket);
				netconfig.openStreams();
 
 
 
				/* utile ce while ?*/
				while(request==null)
				{
					request = (String) netconfig.readObj();
 
				}
 
				waitClient.setText("Connexion : "+request);
				start.setVisible(true);
 
			}
 
		}
De meme que celle là

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package mainMenu;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
 
import java.awt.*;
 
import javax.swing.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.swing.border.*;
 
import Model.GameConstants;
import Model.GameData;
import Model.NetClient;
import Model.NetConfig;
import Test.Log;
 
 
import tableView.TableGUI;
 
/**
 * A panel for launch & option
 */
@SuppressWarnings("serial")
public class Network extends JPanel implements GameConstants {
 
 
 
	private JTextField address;// zone de saisie de texte
	private JTextField port;
 
 
	public TableGUI table;
 
	private InetAddress serverAddress;
 
 
	private JButton launchButton;
 
	public JFrame mainmenu;
 
 
	private NetworkDecision decision;
	private JTextField name;
	private JLabel txt;
	public Socket socket;
	private NetConfig netconfig;
	private GameData game=null; /* init auto si pas ds le construct? */
	private JButton joinButton;
	private Log log;
	// -----------------------------------------------------------------------------------------------------
	/**
         * Create a new panel.Constructeur , agence les 5 sous-zones(composants)
         * selon le lyout cardinal
         * 
         * @param mainmenu
         */
	public Network(NetworkDecision netDecision,JFrame mainmenu,Log log,NetConfig netconfig)
 
	{
		this.log=log;
 
		this.netconfig=netconfig;
 
		this.mainmenu=mainmenu;
		this.decision=netDecision;
 
		setLayout(new GridLayout(3,1)); 
		JLabel title = new JLabel("                                    Configuration réseau");
		title.setFont(new Font("Impact", Font.PLAIN, 24));	
		add(title);
		add(launchPanel());
		add(joinPanel());
 
		this.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.GRAY));
	}
 
	/**
         * -----------------------------------------------------------------------------------
         * A panel containing the "Finished" button. Methode qui retourne un
         * container( comme les autres); Pq une metode = trop style ^^, 
         * non juste une faeon de faire comme une autre 
         */
	private JPanel launchPanel() 
	{
		launchButton = new JButton("Heberger");
		launchButton.addMouseListener(new BullListener("Heberge un jeu."));
		//launchButton.setPreferredSize(new Dimension(400, 70)); // ok
		launchButton.addActionListener(new DoneListener());
 
		JPanel launchPanel = new JPanel();
		launchPanel.add(launchButton);
 
		return launchPanel;
	}
	/**
         * -----------------------------------------------------------------------------------
         * A panel containing the "Finished" button. Methode qui retourne un
         * container( comme les autres); Pq une metode = trop style ^^, 
         * non juste une faeon de faire comme une autre 
         */
	private JPanel joinPanel() 
	{
 
		address = new JTextField(15);
		address.setText("192.168.2.3");
		port = new JTextField(5);
		port.setText(""+PORT);
 
		joinButton = new JButton("Connexion directe");
		joinButton.addMouseListener(new BullListener("Joindre une partie reseau. !"));
		//joinButton.setPreferredSize(new Dimension(400, 70)); // ok
		joinButton.addActionListener(new joinListener());
 
		name = new JTextField(8);
		name.setText("Your name");
 
		txt = new JLabel("IP address");
		JPanel joinPanel = new JPanel();
		joinPanel.add(txt);		
		joinPanel.add(address);
		joinPanel.add(port);
		joinPanel.add(name);
		joinPanel.add(joinButton);
 
		return joinPanel;
	}
 
 
 
 
 
	/*-----------------------------------------------------------------------------------*/
	public void setVisible(Boolean b) {
		this.setVisible(b);
	}
 
	public boolean isOn() {
		return this.isVisible();
	}
 
 
 
	public void setPort(JTextField port) {
		this.port = port;
	}
 
	public JTextField getPort() {
		return port;
	}
	private void netBug() 
	{
		JFrame frame = new JFrame("Message Dialog");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JOptionPane.showMessageDialog(frame, "Erreur Réseau ...", "Attention",
				JOptionPane.WARNING_MESSAGE);
		return;
	}
 
	private void clientLaunch() 
	{
 
 
		game.setNameB(name.getText().concat(SPACES).substring(0, NAME_LENGTH));
		game.setCpu(false);
 
		table = new TableGUI(game, mainmenu,netconfig);
 
	}
 
	//-----------------------------------------------------------------------------------------------------
	/**
         * ------------------------------------------------------------------------------ 
         * An object that
         * handles a press of the Launch button.
         */
	private class DoneListener implements ActionListener, GameConstants 
	{	
 
 
		public void actionPerformed(ActionEvent e) 
		{
			decision.setVisible(true);
			Network.this.setVisible(false);
		}
	}
 
	/*
	 * Direct Connexion
	 */
	public class joinListener implements ActionListener 
	{
 
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			/*Rend l'interface réactive durant la connection, car doit attendre réponse server*/
			/* après X secondes, doit s'arrêter*/
			new Thread(new Connecter(),"ConnecterClient").start();
			joinButton.setEnabled(false);
			txt.setText("Waiting respons ...");
		}
 
 
 
 
 
	}
 
	public static byte[] getIP(String str) 
	{
 
		String[] temp = str.split("[.]");
 
		if (temp.length!=4) 
		{			
			return null;
		}
		byte[] res = new byte[4];
		for(int i=0; i<4; i++)
		{
			try
			{
				res[i]=new Integer(temp[i]).byteValue();
			}
			catch(NumberFormatException e)
			{
				IPerror();
				return null;
			}
		}
 
 
		return res;
 
 
	}
 
	/*
	 * Test
	 */
 
	private static void IPerror() 
	{
		JFrame frame = new JFrame("Message Dialog");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JOptionPane.showMessageDialog(frame, "Adresse IP invalide", "Attention",
				JOptionPane.WARNING_MESSAGE);
		return;
 
	}
 
	/*
	 * TESTS
 
	public static void main(String[] args)
	{		
 
		String test = null;
		try {
			test = InetAddress.getLocalHost().getHostAddress() ;
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
 
		System.out.println(test);
		byte[] add = getIP(test);
 
		try {
			System.out.println(InetAddress.getByAddress(add).getHostAddress().equals(test));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		for(int i=0; i<add.length; i++)
		{
			System.out.println(add[i]);			
		}
	}*/
 
	private class Connecter implements Runnable,GameConstants
	{
 
		public void run()
		{	
			/* Récup et valide l'addresse du champ ip*/
			byte[] ip= getIP(address.getText());
			if (ip==null) { IPerror();}
			try 
			{
				serverAddress =InetAddress.getByAddress(ip) ;
			} 
			catch (UnknownHostException e) 
			{
				netBug();
			}
 
			/* Ouvre un socket vers le serveur*/
			try 
			{
				socket = new Socket(serverAddress,PORT);
 
			} 
			catch (IOException e1) 
			{
				JFrame frame = new JFrame("Message Dialog");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				JOptionPane.showMessageDialog(frame, "Aucune réponse serveur ...", "Attention",
						JOptionPane.WARNING_MESSAGE);
 
				txt.setText("IP Server :");
				joinButton.setEnabled(true);
 
				return;
			}
 
			/*init interfaceur réseau*/
			netconfig.setClient(true);
			netconfig.setSocket(socket);
			log.write("Client Network Connecter :",socket+"");			
			netconfig.openStreams();
 
 
 
 
 
 
			/* Envoi la requete = son nom*/
			String request = name.getText();				
			netconfig.writeObj(request);
 
			/* Attend ensuite la réponse du serveur*/
 
 
 
 
			while(game==null)
			{
				game = (GameData) netconfig.readObj(); /* Instruction bloquante, pas besoin de While?*/
				System.out.println("while");
			}
 
 
			txt.setText("IP Server :");
 
			clientLaunch();			
			joinButton.setEnabled(true);
 
		}
 
	}
 
 
 
 
 
}