62,620
社区成员
发帖
与我相关
我的任务
分享
public class Server1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file=new File("server.txt");
if(file.exists())
file.delete();
String m = "";
ServerSocket ss = new ServerSocket(10002);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "....connected....");
m = ip + "....connected....\n";
InputStream in = s.getInputStream();
int len = 0;
byte[] buf = new byte[1024];
while((len = in.read(buf)) != -1){
m += new String(buf);
};
//System.out.println(new String(buf, 0, len));
OutputStream os=s.getOutputStream();
os.write("饭菜马上到".getBytes());
os.close();
s.close();
ss.close();
FileOutputStream fos = new FileOutputStream(file);
fos.write(m.getBytes());
}
}
public class Client1 {
public static void main(String[] args){
File file=new File("client.txt");
if(file.exists())
file.delete();
try {
Socket s = new Socket("127.0.0.1", 10002);
OutputStream out = s.getOutputStream();
out.write("我肚子饿了".getBytes());
String m = "我肚子饿了\n";
InputStream is = s.getInputStream();
byte buf[] = new byte[1024];
int len = 0;
while((len = is.read(buf)) != -1){
m += new String(buf);
}
//System.out.println(new String(buf, 0, len));
s.close();
FileOutputStream fos = new FileOutputStream(file);
fos.write(m.getBytes());
} catch (Exception e) {
// TODO: handle exception
}
}
}