package com.lovo.client;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
//客户端---消息的发起方
public class TestClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//1、得到要发送的消息,可以接收外部输入
String msg = new Scanner(System.in).next();
//2、得到Socket对象
Socket sc = null;
try {
sc = new Socket("127.0.0.1",9527);
//3、把消息交给Socket对象---由于是发送,所以方向是输出流;发送内容可以是文本也可以是二进制数据,所以它
// 提供的是字节流---综上,一定是OutputStream
// OutputStream out = sc.getOutputStream();
// out.write(msg.getBytes());
// out.flush();//强制输出
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
bw.write(msg);
bw.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//4、关闭
if(sc != null){
try {
sc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}