Send
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Socket socket = new Socket("127.0.0.1",2010);
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write("xiaoming");
bw.flush();
socket.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
Reciver
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
ServerSocket ss = new ServerSocket(2010);
Socket s = ss.accept();
InputStream is = s.getInputStream();
StringBuffer str = new StringBuffer();
int a = -1;
while((a = is.read())!=-1){
str.append((char)a);
}
System.out.print("hello "+str);
ss.close();
}catch(Exception ex){
ex.printStackTrace();
}
}