java 编写网络客户/服务器程序,实现如下功能: (1)设计服务器程序,运行时等待客户端连接; (2)客户
(2)客户端在启动时与服务器建立连接;(3)客户端发送消息,服务器收到该消息后,将其中的所有小写字符转换为大写字符,发回给客户端;(4)客户端收到服务器发回的消息后,在界...
(2)客户端在启动时与服务器建立连接;
(3)客户端发送消息,服务器收到该消息后,将其中的所有小写字符转换为大写字符,发回给客户端;
(4)客户端收到服务器发回的消息后,在界面上显示。
新手木有悬赏分,跪求了!!!! 展开
(3)客户端发送消息,服务器收到该消息后,将其中的所有小写字符转换为大写字符,发回给客户端;
(4)客户端收到服务器发回的消息后,在界面上显示。
新手木有悬赏分,跪求了!!!! 展开
2个回答
展开全部
Server:
package s;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(10001);
while (true) {
Socket socket = server.accept();
System.out.println("a socket in");
new UpderDeal(socket).start();
}
}
}
class UpderDeal extends Thread {
private Socket s = null;
public UpderDeal(Socket s) {
this.s = s;
}
public void run() {
byte[] b = new byte[1024];
String msg = null;
int len = 0;
try {
while (true) {
len = s.getInputStream().read(b);
msg = new String(b, 0, len);
msg = msg.toUpperCase();
s.getOutputStream().write(msg.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
package c;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws UnknownHostException,
IOException {
Socket socket = new Socket("localhost", 10001);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
String msg = null;
Scanner sc = new Scanner(System.in);
new Recieve(in).start();
while (true) {
msg = sc.next();
if (msg.equals("exit")) {
break;
}
System.out.println("发出的信息:" + msg);
out.write(msg.getBytes());
}
}
}
class Recieve extends Thread {
private InputStream in = null;
public Recieve(InputStream in) {
this.in = in;
}
public void run() {
byte[] b = new byte[1024];
int len = 0;
while (true) {
try {
len = in.read(b);
System.out.println("收到的信息为:" + new String(b, 0, len));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
希望可以帮到你,还有一种异步用SocketChannel的,我也怎么用过,可以网上参考一下。
package s;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(10001);
while (true) {
Socket socket = server.accept();
System.out.println("a socket in");
new UpderDeal(socket).start();
}
}
}
class UpderDeal extends Thread {
private Socket s = null;
public UpderDeal(Socket s) {
this.s = s;
}
public void run() {
byte[] b = new byte[1024];
String msg = null;
int len = 0;
try {
while (true) {
len = s.getInputStream().read(b);
msg = new String(b, 0, len);
msg = msg.toUpperCase();
s.getOutputStream().write(msg.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
package c;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws UnknownHostException,
IOException {
Socket socket = new Socket("localhost", 10001);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
String msg = null;
Scanner sc = new Scanner(System.in);
new Recieve(in).start();
while (true) {
msg = sc.next();
if (msg.equals("exit")) {
break;
}
System.out.println("发出的信息:" + msg);
out.write(msg.getBytes());
}
}
}
class Recieve extends Thread {
private InputStream in = null;
public Recieve(InputStream in) {
this.in = in;
}
public void run() {
byte[] b = new byte[1024];
int len = 0;
while (true) {
try {
len = in.read(b);
System.out.println("收到的信息为:" + new String(b, 0, len));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
希望可以帮到你,还有一种异步用SocketChannel的,我也怎么用过,可以网上参考一下。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不就是一个socket编程吗。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |