socket编程

网络编程:字符流
//客户端
public class Cli {
public static void main(String[] args) throws UnknownHostException, IOException {
Thread thread = Thread.currentThread();
System.out.println(thread.getName());

Scanner input =new Scanner(System.in);
//第一步:有打电话的人:Socket
Socket socket =new Socket("127.0.0.1",10087);

//第二步:交流:----io流
//输入流:听
//InputStream in = socket.getInputStream();
BufferedReader br =new BufferedReader(new InputStreamReader(socket.getInputStream()));
//输出流:说
//OutputStream out = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//先听后说
/*byte[] buf = new byte[1024];
in.read(buf);
System.out.println("客服说:"+new String(buf));
//说
System.out.println("请说话:");
String str = input.next();
out.write(str.getBytes());*/

//缓存流
String str = "";
while(true){
if(str.equals("88")){
break;
}
str =br.readLine();
System.out.println("客户说"+str);



System.out.println("请说话");
str = input.next();
bw.write(str);
bw.newLine();
bw.flush();

}
//3.挂电话
bw.close();
br.close();



}

}

//服务端
public class Ser {
public static void main(String[] args) throws IOException {
Scanner input =new Scanner(System.in);
//第一步:装一台总机,公布号码:ServerSocket类
ServerSocket server = new ServerSocket(10087);


//第二步:请很多漂亮的客服MM:Socket
Socket socket = server.accept();


//第三步:交流:io流
//输入流:听
/*InputStream in = socket.getInputStream();
//字节流转为字符流
Reader r = new InputStreamReader(in);*/
//字符流转为缓存流
BufferedReader br =new BufferedReader(new InputStreamReader(socket.getInputStream()));
//输出流:说
//OutputStream out = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//先说后听
/*System.out.println("请说话:");
String sr = input.next();
out.write(sr.getBytes());
//听
byte[] buf =new byte[1024];
int num = in.read(buf);
System.out.println("客户说"+new String(buf));*/

//缓存流
String str = "";
while(true){
if(str.equals("88")){
break;
}
System.out.println("请说话:");
String sr = input.next();
bw.write(sr);
bw.newLine();
bw.flush();

str = br.readLine();
System.out.println("客户说:"+str);
}



//第四步:挂电话
bw.close();
br.close();

}

}

 

网络编程与线程:
public class WriterThread extends Thread {
BufferedWriter bw = null;
public WriterThread(BufferedWriter bw){
this.bw = bw ;
}
@Override
public void run() {
Scanner in =new Scanner(System.in);
while(true){

try {
System.out.println("请说话:");
String string = in.next();

bw.write(string);
bw.newLine();
bw.flush();
if(string.equals("88"))
break;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}


public class ReaderThread extends Thread {
BufferedReader br = null;

public ReaderThread(BufferedReader br){
this.br = br;
}

/*把线程要做的事放到run方法里面就可以了
* start的方法调用。
* */
@Override
public void run() {
while(true){
try {
String str = br.readLine();
System.out.println("对方说:"+str);
if(str.equals("88")){
break;
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}

}

基本内涵:
网络编程的意义在于使电脑之间进行数据共享

网络编程问题:
1.电脑的地址问题:IP与端口号(应用程序)问题
2.数据的传输 io流

posted @ 2017-06-19 17:31  会说话的帆船  阅读(123)  评论(0编辑  收藏  举报