java客户端传递字符串
用户在客户端输入一个字符串,储存在Strings中。通过客户端传送到服务器端,服务器获得该字符串并显示在屏幕上。(都用本机IP)求代码,最好有注释...
用户在客户端输入一个字符串,储存在String s中。
通过客户端传送到服务器端,服务器获得该字符串并显示在屏幕上。(都用本机IP)
求代码,最好有注释 展开
通过客户端传送到服务器端,服务器获得该字符串并显示在屏幕上。(都用本机IP)
求代码,最好有注释 展开
1个回答
展开全部
public MyClient() {
try {
Socket s = new Socket("127.0.0.1", 9999);
ObjectOutputStream oop=new ObjectOutputStream(s.getOutputStream());
User u = new User();
u.setId(1);
u.setPass(123);
oop.writeObject(u);
oop.flush();
oop.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Server() {
try {
System.out.println("ヤレ999シ猯");
ServerSocket ss = new ServerSocket(9999);
while (true) {
Socket s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s
.getInputStream());
User u = (User) ois.readObject();
Message m = new Message();
ObjectOutputStream oos = new ObjectOutputStream(s
.getOutputStream());
System.out.println("エモソヘサァカヒスモハワオス" + u.getId() + "テワツ・ + u.getPass());
if (u.getPass().equals("123456")) {
m.setStlye("1");
oos.writeObject(m);
QqerverClientThread qct = new QqerverClientThread(s);
ManegerClientThread.addThread(u.getId(), qct);
qct.start();
} else {
m.setStlye("2");
oos.writeObject(m);
s.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
一个写一个读 写到两个类中
try {
Socket s = new Socket("127.0.0.1", 9999);
ObjectOutputStream oop=new ObjectOutputStream(s.getOutputStream());
User u = new User();
u.setId(1);
u.setPass(123);
oop.writeObject(u);
oop.flush();
oop.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Server() {
try {
System.out.println("ヤレ999シ猯");
ServerSocket ss = new ServerSocket(9999);
while (true) {
Socket s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s
.getInputStream());
User u = (User) ois.readObject();
Message m = new Message();
ObjectOutputStream oos = new ObjectOutputStream(s
.getOutputStream());
System.out.println("エモソヘサァカヒスモハワオス" + u.getId() + "テワツ・ + u.getPass());
if (u.getPass().equals("123456")) {
m.setStlye("1");
oos.writeObject(m);
QqerverClientThread qct = new QqerverClientThread(s);
ManegerClientThread.addThread(u.getId(), qct);
qct.start();
} else {
m.setStlye("2");
oos.writeObject(m);
s.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
一个写一个读 写到两个类中
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询