-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSendMultipleMessages.java
More file actions
55 lines (40 loc) · 1.59 KB
/
SendMultipleMessages.java
File metadata and controls
55 lines (40 loc) · 1.59 KB
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
import java.io.PrintWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public class SendMultipleMessages {
private static void sendData(int totalBytes, int messageSizeInBytes)
throws IOException {
// Modify this to match to be THIS computer's address
String ipAddress = "127.0.0.1";
int serverPort = 10000;
InetAddress serverAddress = InetAddress.getByName(ipAddress);
int messagesToSend = totalBytes / messageSizeInBytes;
String myData = "";
for(int i = 0; i < messageSizeInBytes; i++) {
myData += "X";
}
Socket socket = new Socket(serverAddress, serverPort);
// Try changing this to false and see if it changes how packets are sent
boolean autoFlush = true;
PrintWriter out = new PrintWriter(socket.getOutputStream(), autoFlush);
for (int i = 0; i < messagesToSend; i++) {
out.print(myData);
// Try enabling this and see if it changes how packets are sent
// out.flush();
}
out.print("\n");
out.flush();
socket.close();
}
public static void main(String[] args) throws Exception {
// How many bytes to send
int totalBytes = 5000;
// How big each message will be
int messageSizeInBytes = 10;
System.out.println("Sending " + totalBytes + "bytes in " + messageSizeInBytes + " byte chunks.");
// Send the data
sendData(totalBytes, messageSizeInBytes);
System.out.println("Done sending message.");
}
}