发送:
public void sendMsg(){Socket socket = new Socket(IP地址,port);
// 向服务端程序发送数据 OutputStream os = socket.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw);
String msg=“字符串” bw.write(msg);
bw.flush();
}
接收:public void receiveMsg()Socket socket = new Socket(IP地址,port);
InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String msg="";while((msg=br.readLine())!=null){System.out.println(msg);
}socket.close();
}