1、简单Client 输入(TCP)
public class TestServer {
public static void main(String []args)throws Exception{
ServerSocket ss=new ServerSocket(1024);
System.out.println("Server");
Socket so=ss.accept();
DataInputStream dis =new DataInputStream(so.getInputStream());
DataOutputStream dos =new DataOutputStream(so.getOutputStream());
while(true){
String temp=dis.readUTF();
if(temp.equalsIgnoreCase("bye"))break;
dos.writeUTF("server:"+temp);
}
dis.close();
dos.close();
so.close();
ss.close();
}
}
public class TestClient {
public static void main(String[] args) throws Exception {
Socket so = new Socket("127.0.0.1", 1024);
System.out.println("Client");
DataInputStream dis = new DataInputStream(so.getInputStream());
DataOutputStream dos = new DataOutputStream(so.getOutputStream());
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
while(true){
String temp= br.readLine();
dos.writeUTF(temp);
if(temp.equalsIgnoreCase("bye"))break;
System.out.print(dis.readUTF());
}
br.close();
dis.close();
dos.close();
so.close();
}
}
2、多个客户端 服务器并发处理(TCP)
public class TestClient {
public static void main(String[] args) throws Exception {
Socket so = new Socket("127.0.0.1", 1024);
System.out.println("Client");
DataInputStream dis = new DataInputStream(so.getInputStream());
DataOutputStream dos = new DataOutputStream(so.getOutputStream());
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
while(true){
String temp= br.readLine();
dos.writeUTF(temp);
if(temp.equalsIgnoreCase("bye"))break;
System.out.print(dis.readUTF());
}
br.close();
dis.close();
dos.close();
so.close();
}
}
public class TestServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(1024);
System.out.println("Server");
while (true) {
Socket so = ss.accept();
new ServerThread(so).start();
}
//ss.close();服务器用不关闭
}
}
public class ServerThread extends Thread {
private Socket so;
private static String word="";
public ServerThread(Socket so) {
this.so = so;
}
public void run() {
try {
DataInputStream dis = new DataInputStream(so.getInputStream());
DataOutputStream dos = new DataOutputStream(so.getOutputStream());
while (true) {
String temp = dis.readUTF();
word=word+temp+"\n";
if (temp.equalsIgnoreCase("bye"))
break;
dos.writeUTF(word);
}
dis.close();
dos.close();
so.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3 (UDP)
IP的设置方法
public class TestInetAddress {
public static void main(String[] args) throws Exception {
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia);
byte[] b = ia.getAddress();
for (byte bb : b)
System.out.println(bb);
// ////////////
// 192.168.1.200
byte[] by = { 192 - 256, 168 - 256, 1, 200 - 256 };
InetAddress ia2 = InetAddress.getByAddress(by);
System.out.println(ia2);
}
}
自己写的
public class Receive {
public static void main(String[] args) throws Exception {
System.out.println("Receive:");
DatagramSocket ds = new DatagramSocket(8848);
byte[] b = new byte[2048];
DatagramPacket dp = new DatagramPacket(b, b.length);
while (true) {
ds.receive(dp);// 停住
String temp = new String(b, 0, dp.getLength());
if (temp.equalsIgnoreCase("bye"))
break;
System.out.println(temp);
}
ds.close();
}
}
public class Send {
public static void main(String[] args) throws Exception {
System.out.println("Send:");
byte[] b = new byte[2048];
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress
.getLocalHost(), 8848);
DatagramSocket ds = new DatagramSocket();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while (true) {
String temp = br.readLine();
dp.setData(temp.getBytes());
ds.send(dp);
if (temp.equalsIgnoreCase("bye"))
break;
}
br.close();
ds.close();
}
}
老师写的
public class Receive {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(8848); // 邮递员
byte[] b = new byte[2048];
DatagramPacket dp = new DatagramPacket(b, b.length);// 邮筒
while (true) {
ds.receive(dp);// 停住,直到收到信
String temp = new String(b, 0, dp.getLength());
System.out.println(temp);
if (temp.equals("bye"))
break;
}
ds.close();
}
}
public class Send {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
DatagramSocket ds = new //邮递员
DatagramSocket();
while (true) {
String temp = br.readLine();
byte[] b = temp.getBytes();//写信
DatagramPacket dp = new //装好信
DatagramPacket(b, b.length, InetAddress.getLocalHost(), 8848);
ds.send(dp);//发信
if (temp.equals("bye"))
break;
}
ds.close();
}
}
4、public class TestURL {
//读网页的内容,很重要。面试要考试
public static void main(String[] args) throws Exception {
URL url = new URL("http://bbs.tarena.com.cn");
URLConnection uc = url.openConnection();
InputStreamReader isr = new InputStreamReader(uc.getInputStream());
BufferedReader br = new BufferedReader(isr);
while (true) {
String temp = br.readLine();
if (temp == null)
break;
System.out.println(temp);
}
br.close();
}
}