import java.net.*;
public class MyServer {
public static void main(String[] args){
try {
ServerSocket ss = new ServerSocket(8000);
System.out.println("监听前.......");
Socket s = ss.accept();
System.out.println("监听后.......");
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.net.*;
public class MyClient {
public static void main(String[] agrs){
try {
Socket s = new Socket("127.0.0.1", 8000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.net.*;
public class MyServer {
public static void main(String[] args){
try {
ServerSocket ss = new ServerSocket(8000);
System.out.println("监听前.......");
Socket s = ss.accept();
System.out.println(s.getInputStream().read());
System.out.println("监听后.......");
} catch (Exception e) {
// TODO: handle exception
}
}
}
我们用s来得到输入流,然后用这个输入流读。到这里,客户端代码你应该琢磨着就能写出来了。
import java.net.*;
public class MyClient {
public static void main(String[] agrs){
try {
Socket s = new Socket("127.0.0.1", 8000);
s.getOutputStream().write(100);
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.net.*;
import java.io.*;
public class MyServer {
public static void main(String[] args){
try {
ServerSocket ss = new ServerSocket(8000);
System.out.println("监听前.......");
Socket s = ss.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.println(br.readLine());
System.out.println("监听后.......");
} catch (Exception e) {
// TODO: handle exception
}
}
}
我们得到了字节流,然后转换成字符流,最后得到能够读一行数据的高级流。下面的客户端代码你最好先自己试着写一下,然后再看我的。
import java.net.*;
import java.io.*;
public class MyClient {
public static void main(String[] agrs){
try {
Socket s = new Socket("127.0.0.1", 8000);
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw);
pw.println("王洋");
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.net.*;
import java.io.*;
public class MyClient {
public static void main(String[] agrs){
try {
Socket s = new Socket("127.0.0.1", 8000);
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw,true);
pw.println("Java学习。");
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.net.*;
import java.io.*;
public class MyServer {
public static void main(String[] args){
try {
ServerSocket ss = new ServerSocket(8000);
System.out.println("监听前.......");
Socket s = ss.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String name = br.readLine();
System.out.println(name);
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw, true);
pw.println("欢迎"+name);
System.out.println("监听后.......");
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.net.*;
import java.io.*;
public class MyClient {
public static void main(String[] agrs){
try {
Socket s = new Socket("127.0.0.1", 8000);
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw,true);
pw.println("王洋");
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String mess = br.readLine();
System.out.println(mess);
} catch (Exception e) {
// TODO: handle exception
}
}
}