Bonjour, j'essaye de découvrir les sockets et datagram java et je suis tombé sur un exercice d'application. voici l'énoncé :
Un datagramme conforme à ce format devra comporter les champs suivants :
– type : un octet unique qui prendra la valeur 0x04
– source : nom de login source, représenté par un tableau d’octets de taille fixe (16 octets), précédé d’un octet
indiquant le nombre d’octets significatifs présents dans le tableau
– target : nom de login destinataire, représenté par un tableau d’octets de taille fixe (16 octets), précédé d’un
octet indiquant le nombre d’octets significatifs présents dans le tableau
– data : une séquence de données d’au plus 4 Ko précédée d’un octet indiquant la longueur de la séquence
exprimée en mots de 16 octets , taille de data = 16 x l avec l la longueur du data
forme du datagram :
type | n | source | m | target | l | data
Vous devez écrire deux programmes (un émetteur et un récepteur) qui s’échangent des messages, en utilisant
des datagrammes conformes au format n ̊ 4 : l’émetteur envoie un datagramme contenant des données saisies par
un utilisateur ; le récepteur reçoit des datagrammes et affiche à l’écran les messages qu’il reçoit.
L’émetteur lit des lignes de texte saisies par l’utilisateur, terminées par une ligne commençant par un point :


> java SendMessage <adresse> <port> <loginSource> <loginDest>
blablabla
blabla
bla
.
Le récepteur affiche les messages qu’il reçoit :
> java RecvMessage <port>
From: machin
To: bidule
Date: mercredi 18 mars 2009, 11:24:54 (UTC+0100)
blablabla
blabla
bla
Je dispose d'un classe DataBufferizer dont voici le code
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
 /*
 
 * File    : $DataBufferizer.java$ 
 * Version : $1.1$
 * Date    : $Wed Mar 18 10:37:54 CET 2009$
 */
 
public class DataBufferizer {
 
	private static char[] digits = {'0','1','2','3','4','5','6','7','8','9',
		'A','B','C','D','E','F'};
 
	//------------------------------------------------------------
	public static boolean readBoolean(byte[] buffer, int offset) {
		return (buffer[offset] != 0);
	}
 
	//------------------------------------------------------------
	public static void writeBoolean(boolean v, byte[] buffer, int offset) {
		if (v) buffer[offset] = 1;
		else buffer[offset] = 0;
	}
 
	//------------------------------------------------------------
	public static byte readByte(byte[] buffer, int offset) {
		return buffer[offset];
	}
 
	//------------------------------------------------------------
	public static void writeByte(byte v, byte[] buffer, int offset) {
		buffer[offset] = v;
	}
 
	//------------------------------------------------------------
	public static short readUnsignedByte(byte[] buffer, int offset) {
		short	val = (short)(buffer[offset] & 0x00FF);
		return val;
	}
 
	//------------------------------------------------------------
	public static void writeUnsignedByte(short v, byte[] buffer, int offset) {
		buffer[offset] = (byte)(0x00FF & v);
	}
 
	//------------------------------------------------------------
	public static byte[] readByteArray(byte[] buffer, int offset, int length) {
		byte[] val = new byte[length];
		System.arraycopy(buffer,offset,val,0,length);
		return val;
	}
 
	//------------------------------------------------------------
	public static void writeByteArray(byte[] v, byte[] buffer, int offset, int length) {
		System.arraycopy(v,0,buffer,offset,length);
	}
 
	//------------------------------------------------------------
	public static char readChar(byte[] buffer, int offset) {
		int val = buffer[offset] << 8;
		val |= buffer[offset+1];
		return ((char)val);
	}
 
	//------------------------------------------------------------
	public static void writeChar(char v, byte[] buffer, int offset) {
		buffer[offset] = (byte)((v & 0xFF00) >> 8);
		buffer[offset+1] = (byte)((v & 0x00FF));		
	}
 
	//------------------------------------------------------------
	public static double readDouble(byte[] buffer, int offset) {
		return Double.longBitsToDouble(readLong(buffer, offset));
	}
 
	//------------------------------------------------------------
	public static void writeDouble(double v, byte[] buffer, int offset) {
		writeLong(Double.doubleToLongBits(v), buffer, offset);
	}
 
	//------------------------------------------------------------
	public static float readFloat(byte[] buffer, int offset)  {
		return Float.intBitsToFloat(readInt(buffer, offset));
	}
 
	//------------------------------------------------------------
	public static void writeFloat(float v, byte[] buffer, int offset) {
		writeInt(Float.floatToIntBits(v), buffer, offset);
	}
 
	//------------------------------------------------------------
	public static int readInt(byte[] buffer, int offset) {
		int value = ((int)buffer[offset] & 0xFF) << 24;
		value |= ((int)buffer[offset+1] & 0xFF) << 16;
		value |= ((int)buffer[offset+2] & 0xFF) << 8;
		value |= ((int)buffer[offset+3] & 0xFF);
		return value;
	}
 
	//------------------------------------------------------------
	public static void writeInt(int v, byte[] buffer, int offset) {
 
		buffer[offset] = (byte)(0xFF & (v >> 24));
		buffer[offset+1] = (byte)(0xFF & (v >> 16));
		buffer[offset+2] = (byte)(0xFF & (v >> 8));
		buffer[offset+3] = (byte)(0xFF & v);
	}
 
	//------------------------------------------------------------
	public static long readLong(byte[] buffer, int offset) {
		return ((long)readInt(buffer, offset) << 32) 
		| ((long)readInt(buffer, offset+4) & 0xffffffffL);
	}
 
	//------------------------------------------------------------
	public static void writeLong(long v, byte[] buffer, int offset) {
		int hiInt=(int)(v >> 32);
		int loInt=(int)(v & 0xFFFFFFFF);
 
		writeInt(hiInt, buffer, offset);
		writeInt(loInt, buffer, offset+4);
	}
 
	//------------------------------------------------------------
	public static short readShort(byte[] buffer, int offset) {
		short val = (short)((buffer[offset] & 0xFF) << 8);
		val |= ((short)buffer[offset+1] & 0xFF);
		return val;
	}
 
	//------------------------------------------------------------
	public static void writeShort(short v, byte[] buffer, int offset) {
		buffer[offset] = (byte)(0xFF & (v >> 8));
		buffer[offset+1] = (byte)(0xFF & v);
	}
 
	//------------------------------------------------------------
	public static short getBytesLenght(String str) {
		return (short)str.getBytes().length;
	}
 
	//------------------------------------------------------------
	public static String readString(byte[] buffer, int offset) {
		short length = readUnsignedByte(buffer, offset);
		return new String(buffer, offset+1, length);
	}
 
	//------------------------------------------------------------
	public static void writeString(String str, byte[] buffer, int offset) {
		byte[] strBuffer = str.getBytes();
		int length = strBuffer.length;
		if (length > Short.MAX_VALUE) length = Short.MAX_VALUE;
		writeUnsignedByte((short)length, buffer, offset);
		System.arraycopy(strBuffer, 0, buffer, offset+1, length);
	}
 
	//------------------------------------------------------------
	public static String bufferToString(byte[] buffer) {
		return bufferToString(buffer, 0, buffer.length);
	}
 
	//------------------------------------------------------------
	public static String bufferToString(byte[] buffer, 
			int offset, 
			int length) {
		StringBuffer result = new StringBuffer();
		int max = (length+offset>buffer.length?buffer.length:length+offset);
		for (int i=offset; i<max; i++)
			result.append(hexa(buffer[i]) + " ");
		return result.toString();
	}
 
//	------------------------------------------------------------
	public static String bufferToBinaryString(byte[] buffer) {
		return bufferToBinaryString(buffer, 0, buffer.length);
	}
 
	//------------------------------------------------------------
	public static String bufferToBinaryString(byte[] buffer, 
			int offset, 
			int length) {
		StringBuffer result = new StringBuffer();
		int max = (length+offset>buffer.length?buffer.length:length+offset);
		for (int i=offset; i<max; i++)
			result.append(bin(buffer[i]) + " ");
		return result.toString();
	}
 
	//------------------------------------------------------------
	public static String hexa(byte v) {
		int low  = v & 0x0F;
		int high = (v & 0xF0) >> 4;
		return String.valueOf(digits[high]) + String.valueOf(digits[low]);
	}
 
	//------------------------------------------------------------
	public static String bin(byte v) {
		String s = "";
		for (int i=0;i<8;i++) {
			if ((v & 0x01) == 1) s = "1" + s; else s = "0" + s;
			v = (byte)(v >> 1);
		}
		return s;
	}
 
	//------------------------------------------------------------
	public static boolean compareBuffer(byte[] sourceBuffer, 
			int sourceOffset, 
			byte[] toBuffer, 
			int toOffset, 
			int length) {
		for (int i=0; i<length; i++)
			if (toBuffer[toOffset + i] != sourceBuffer[sourceOffset + i])
				return false;
		return true;
	}
 
}
n et m sont prit en compte avec login et target grâce a la méthode writeString, qui écrit un n° et le login. Mon problème est de savoir comment gérer le champ data, sachant que le message est tapé comme dans l'énoncé.... (c'est à dire après avoir écris les paramètres et prendre en compte la fin de la saisie grâce au point) Voici mon travail pour le moment sur la partie SendMessage :
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
import java.net.*;
import java.io.*;
 
import java.util.Date;
 
 
 
public class SendMessage {
 
	public DataBufferizer dataBufferizer;
	public String adresse1;
	public int port1;
 
 
	public SendUserInfo(String host, int port) throws Exception{
		this.adresse1 = host;
		this.port1 = port;
 
	}
 
	public void send(String source, String target) throws IOException {
		byte[] buffer = new byte[32];
		byte[] stringBuffer = new byte[];
		byte type = 1;
		DataBufferizer.writeByte(type, buffer, 0);  //type
 
		long date = new Date().getTime(); //date
 
		DataBufferizer.writeString(source, buffer, 1); //numero et login de la source
		DataBufferizer.writeString(target, buffer, 18); //numero et login du destinataire
 
 
 
		System.out.println("type : " + type);
		System.out.println("date : " + new Date(date));
		System.out.println("Emetteur : " +source);
		System.out.println("Recepteur : " +target);
 
 
		InetAddress adresseIP = InetAddress.getByName(adresse1);
		DatagramSocket socket = new DatagramSocket();
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length, adresseIP, port1);
		socket.send(packet);
		socket.close();
	}
 
 
 
 
	public static void main(String[] args) throws Exception{
	 (pas encore complété)
	}
 
}

Merci d'avance pour toute aide que vous pourriez m'apporter.