admin 管理员组

文章数量: 1086019


2023年12月22日发(作者:gamma函数的使用方法)

package myfistsocket;

/*一个作为Server*/

import .*;

import .*;

class MynewServer

{

public static void main(String[] args) throws IOException

{

ServerSocket s = new ServerSocket(6666);

n("服务器端------监听中.....");

Socket socket = ();

n("开始:" + socket);

GetMessage gm = new GetMessage(socket);

SendMessage sm = new SendMessage(socket);

Thread gt = new Thread(gm);

Thread st = new Thread(sm);

();

();

}

}

// 接收消息

class GetMessage implements Runnable

{

BufferedReader in;

public GetMessage(Socket socket)

{

try

{

in = new BufferedReader(new

InputStreamReader(utStream()));

}

catch (IOException e)

{

tackTrace();

}

}

public void run()

{

String str = "";

while (true)

{

try

{

str = ne();

}

catch (IOException e)

{

tackTrace();

}

if (("q"))

{

break;

}

("客户端回应说:" + str);

}

}

}

// 发送消息

class SendMessage implements Runnable

{

PrintWriter out;

BufferedReader is;

public SendMessage(Socket socket) throws IOException

{

out = new PrintWriter(new BufferedWriter(new

OutputStreamWriter(putStream())), true);

is = new BufferedReader(new

InputStreamReader());

}

public void run()

{

String input = new String();

while (true)

{

try

{

input = ne().trim();

}

catch (IOException e)

{

tackTrace();

}

n(input);

("服务器说:");

();

}

}

}

package myfistsocket;

/*一个作为Client*/

import .*;

import .*;

public class MynewClient

{

public static void main(String[] args) throws IOException

{

InetAddress addr = ame("127.0.0.1");

Socket socket = new Socket(addr, 6666);

n("客户端发出socket=" + socket);

CGetMessage gm = new CGetMessage(socket);

CSendMessage sm = new CSendMessage(socket);

Thread gt = new Thread(gm);

Thread st = new Thread(sm);

();

();

}

}

// 接收消息

class CGetMessage implements Runnable

{

private Socket socket;

BufferedReader in;

public CGetMessage(Socket socket)

{

= socket;

}

public void run()

{

BufferedReader in;

String str = "";

while (true)

{

try

{

in = new BufferedReader(new

InputStreamReader(utStream()));

str = ne();

}

catch (IOException e)

{

tackTrace();

}

n("服务器回应说:" + str);

}

}

}

// 发送消息

class CSendMessage implements Runnable

{

PrintWriter out;

BufferedReader is;

public CSendMessage(Socket socket) throws IOException

{

out = new PrintWriter(new BufferedWriter(new

OutputStreamWriter(putStream())), true);

is = new BufferedReader(new

InputStreamReader());

}

public void run()

{

String input = new String();

while (true)

{

try

{

input = ne().trim();

}

catch (IOException e)

{

tackTrace();

}

n(input);

("客户端说:");

();

}

}


本文标签: 函数 消息 客户端 回应 方法