![]() |
#2
jinjoxie2016-06-21 09:25
|
客户端会给服务器发送一个字符串服务器会把这个字符串转换成大写形式发回给客户端并由客户端显示,同时,服务器会告知客户端,他是第几个客户端。

import *;
import *;
public class Server {
static int i=1;
public Server() {
try {
ServerSocket ss = new ServerSocket(11038);
Socket s = ss.accept();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeUTF("hihi");
System.out.println("原字母为:"+dis.readUTF());
dos.flush();
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
}
}
public static void main(String[] args) {
new Server();
while(true){
System.out.println("你是第"+i+"个客户端");
i++;
}
}
}
import *;
public class Server {
static int i=1;
public Server() {
try {
ServerSocket ss = new ServerSocket(11038);
Socket s = ss.accept();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeUTF("hihi");
System.out.println("原字母为:"+dis.readUTF());
dos.flush();
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
}
}
public static void main(String[] args) {
new Server();
while(true){
System.out.println("你是第"+i+"个客户端");
i++;
}
}
}

import *;
import *;
public class client {
public client() {
try {
Socket s = new Socket("localhost", 11038);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeUTF("hello");
String info = dis.readUTF().toUpperCase();
System.out.println("转化为大写字母后为:"+info);
dos.flush();
dos.close();
dis.close();
s.close();
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
}
}
public static void main(String args[]) {
new client();
}
}
import *;
public class client {
public client() {
try {
Socket s = new Socket("localhost", 11038);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeUTF("hello");
String info = dis.readUTF().toUpperCase();
System.out.println("转化为大写字母后为:"+info);
dos.flush();
dos.close();
dis.close();
s.close();
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
}
}
public static void main(String args[]) {
new client();
}
}
运行后,程序就跑不停,各位大神帮我看看该如何