使用DataOutputStream的纯字符串UTF-8通信
/**来自 NowJava.com - 时 代 Java**/ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; public class Client { private static final Logger logger = LogManager.getLogger(Client.class); public static void main(String [] args) throws NoSuchProviderException, NoSuchAlgorithmException { if (args.length != 2) { System.err.println("Usage: java Client <host name> <port number>"); System.exit(1);/** 来自 N o w J a v a . c o m**/ } String hostName = args[0]; String str = "Hello World!"; int portNumber = Integer.parseInt(args[1]); // Communication try{ // Sockets Socket socket = new Socket(hostName, portNumber);