展开全部
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//服务器 开启一个 socket ,port 2008
ServerSocket server = new ServerSocket(2008);
while(true)
{
Socket client = server.accept();
new ServerThread(client).start();
Thread.sleep(100);
}
}
}
class ServerThread extends Thread{
Socket socket;
DataInputStream in;
DataOutputStream out;
public ServerThread(Socket socket) throws Exception {
this.socket = socket;
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
@Override
public void run() {
String str = null;
while(true)
{
try {
str = in.readUTF();
translateMsg(str);
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void translateMsg(String msg){
System.out.println(msg);
}
}
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//服务器 开启一个 socket ,port 2008
ServerSocket server = new ServerSocket(2008);
while(true)
{
Socket client = server.accept();
new ServerThread(client).start();
Thread.sleep(100);
}
}
}
class ServerThread extends Thread{
Socket socket;
DataInputStream in;
DataOutputStream out;
public ServerThread(Socket socket) throws Exception {
this.socket = socket;
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
@Override
public void run() {
String str = null;
while(true)
{
try {
str = in.readUTF();
translateMsg(str);
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void translateMsg(String msg){
System.out.println(msg);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如下:
while(true){
ServerSocket mSocket = new ServerSocket(1234);
Client c = new Client(mSocket);
}
class Client implements Runable{
Socket s;
public Client(Socket s){
this.s = s;}
public void run(){
// 你的业务逻辑
}
}
while(true){
ServerSocket mSocket = new ServerSocket(1234);
Client c = new Client(mSocket);
}
class Client implements Runable{
Socket s;
public Client(Socket s){
this.s = s;}
public void run(){
// 你的业务逻辑
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询