java socket 问题
请帮忙写一个javasocket客户端和服务器端,客户端每隔5秒向服务器端发送一个字符串,服务器将每次得到客户端的字符串返回并再发送一个整数,要求服务器至少能连5个客户端...
请帮忙写一个java socket 客户端 和服务器端, 客户端每隔5秒 向服务器端发送一个字符串,服务器 将每次得到客户端的 字符串返回 并 再发送一个 整数 ,要求 服务器至少能连5个客户端
要求 只建立一个连接 不关闭连接 展开
要求 只建立一个连接 不关闭连接 展开
展开全部
服务端:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public void listen() throws IOException
{
ServerSocket server = new ServerSocket(8002);
while (true)
{
try
{
Socket client = server.accept();
if (client != null)
{
ServerProcessor processor = new ServerProcessor(client);
processor.start();
}
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException
{
Server s = new Server();
s.listen();
}
class ServerProcessor extends Thread
{
private DataInputStream in;
private DataOutputStream out;
public ServerProcessor(Socket client) throws IOException
{
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
}
public void run()
{
while (true)
{
try
{
String message = in.readUTF();
System.out.println(message);
out.writeUTF(message);
out.writeInt(0);
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
break;
}
}
}
}
}
客户端:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
{
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;
public Client() throws UnknownHostException, IOException
{
socket = new Socket("127.0.0.1", 8002);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
}
public void run()
{
new ClientWriter().start();
new ClientReader().start();
}
class ClientWriter extends Thread
{
public void run()
{
while (true)
{
try
{
dos.writeUTF("发送一个字符串");
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
class ClientReader extends Thread
{
public void run()
{
while (true)
{
try
{
String message = dis.readUTF();
System.out.println(message);
int num = dis.readInt();
System.out.println(num);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws UnknownHostException, IOException
{
Client c = new Client();
c.run();
}
}
当然,发送的字符串和整数你可以改变,我只是拿"发送一个字符串"与0进行说明,还有就是监听的端口号等都可以根据你的实际情况改变。希望对你有所帮助!
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public void listen() throws IOException
{
ServerSocket server = new ServerSocket(8002);
while (true)
{
try
{
Socket client = server.accept();
if (client != null)
{
ServerProcessor processor = new ServerProcessor(client);
processor.start();
}
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException
{
Server s = new Server();
s.listen();
}
class ServerProcessor extends Thread
{
private DataInputStream in;
private DataOutputStream out;
public ServerProcessor(Socket client) throws IOException
{
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
}
public void run()
{
while (true)
{
try
{
String message = in.readUTF();
System.out.println(message);
out.writeUTF(message);
out.writeInt(0);
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
break;
}
}
}
}
}
客户端:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
{
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;
public Client() throws UnknownHostException, IOException
{
socket = new Socket("127.0.0.1", 8002);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
}
public void run()
{
new ClientWriter().start();
new ClientReader().start();
}
class ClientWriter extends Thread
{
public void run()
{
while (true)
{
try
{
dos.writeUTF("发送一个字符串");
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
class ClientReader extends Thread
{
public void run()
{
while (true)
{
try
{
String message = dis.readUTF();
System.out.println(message);
int num = dis.readInt();
System.out.println(num);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws UnknownHostException, IOException
{
Client c = new Client();
c.run();
}
}
当然,发送的字符串和整数你可以改变,我只是拿"发送一个字符串"与0进行说明,还有就是监听的端口号等都可以根据你的实际情况改变。希望对你有所帮助!
展开全部
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Area {
public static final int PORT = 22222;
public static void main(String[] args) throws Exception {
new Server();
for (int i = 0; i < 5; i++) {
new Client("test" + i);
}
}
static class Server {
public Server() {
// 创建服务器
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() throws Exception {
// 监听端口
final ServerSocket ss = new ServerSocket(PORT);
new Thread(new Runnable() {
public void run() {
while (true) {
try {
final Socket client = ss.accept();
new Thread(new Runnable() {
public void run() {
try {
DataInputStream dis = new DataInputStream(
client.getInputStream());
DataOutputStream dos = new DataOutputStream(
client.getOutputStream());
while (true) {
String str = dis.readUTF();
dos.writeUTF(str);
System.out.println("receive :"
+ str);
dos.writeInt((int) (Math.random() * 1000));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}).start();
}
}
static class Client {
private String str;
public Client(String str) {
this.str = str;
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() throws Exception {
final Socket socket = new Socket("localhost", PORT);
new Thread(new Runnable() {
public void run() {
try {
DataOutputStream dos = new DataOutputStream(
socket.getOutputStream());
while (true) {
dos.writeUTF(str);
Thread.sleep(5000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
}
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Area {
public static final int PORT = 22222;
public static void main(String[] args) throws Exception {
new Server();
for (int i = 0; i < 5; i++) {
new Client("test" + i);
}
}
static class Server {
public Server() {
// 创建服务器
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() throws Exception {
// 监听端口
final ServerSocket ss = new ServerSocket(PORT);
new Thread(new Runnable() {
public void run() {
while (true) {
try {
final Socket client = ss.accept();
new Thread(new Runnable() {
public void run() {
try {
DataInputStream dis = new DataInputStream(
client.getInputStream());
DataOutputStream dos = new DataOutputStream(
client.getOutputStream());
while (true) {
String str = dis.readUTF();
dos.writeUTF(str);
System.out.println("receive :"
+ str);
dos.writeInt((int) (Math.random() * 1000));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}).start();
}
}
static class Client {
private String str;
public Client(String str) {
this.str = str;
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() throws Exception {
final Socket socket = new Socket("localhost", PORT);
new Thread(new Runnable() {
public void run() {
try {
DataOutputStream dos = new DataOutputStream(
socket.getOutputStream());
while (true) {
dos.writeUTF(str);
Thread.sleep(5000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询