Created
October 25, 2011 17:05
-
-
Save freewind/1313511 to your computer and use it in GitHub Desktop.
Simple socket server and client, to test the speed of java socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream; | |
import java.net.Socket; | |
public class SimpleClient { | |
public static void main(String[] args) throws Exception { | |
Socket socket = new Socket("127.0.0.1", 6666); | |
InputStream input = socket.getInputStream(); | |
long total = 0; | |
long start = System.currentTimeMillis(); | |
byte[] bytes = new byte[10240]; // 10K | |
while (true) { | |
int read = input.read(bytes); | |
total += read; | |
long cost = System.currentTimeMillis() - start; | |
if (cost > 0 && System.currentTimeMillis() % 10 == 0) { | |
System.out.println("Read " + total + " bytes, speed: " + total / cost + "KB/s"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class SimpleServer { | |
public static void main(String[] args) throws Exception { | |
ServerSocket server = new ServerSocket(6666); | |
Socket socket = server.accept(); | |
OutputStream output = socket.getOutputStream(); | |
byte[] bytes = new byte[10 * 1024]; // 10K | |
for (int i = 0; i < bytes.length; i++) { | |
bytes[i] = 12; | |
} | |
while (true) { | |
output.write(bytes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment