JAvaルーチン練習(ネットワークプログラミング[簡易ネットワーク接続試験])
1590 ワード
import java.net.*;
import java.io.*;
public class TestTCPServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(6666);//
while(true) {
// !
// Socket s = ss.accept();
// DataInputStream dis =
// new DataInputStream(s.getInputStream());
// System.out.println(dis.readUTF());//
// dis.close();
// s.close();
Socket s1 = ss.accept();
OutputStream os = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("Hello," + s1.getInetAddress() +
"port#" + s1.getPort()+ " bye-bye!");
dos.close();
s1.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println(" : " + e);
}
}
}
import java.net.*;
import java.io.*;
public class TestTCPClient {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 6666);
// !
// OutputStream os = s.getOutputStream();
// DataOutputStream dos = new DataOutputStream(os);
//
// Thread.sleep(3000);
// dos.writeUTF("Hello Server!");
// dos.flush();
// dos.close();
// s.close();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
dis.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}