java socket网络编程run_利用Socket进行Java网络编程

import java.net.*;

import java.io.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class talkServer

{ public static void main(String[] args)

{ try

{ file://建立服务器

ServerSocket server = new ServerSocket(9998);

int i=1;

for(;;)

{ Socket incoming = server.accept();

new ServerThread(incoming,i).start();

i++;

}

}catch (IOException ex){

ex.printStackTrace();

}

}

}

class ServerThread extends Thread implements ActionListener

{

private int threadNum;

private Socket socket;

talkServerFrm t;

BufferedReader in;

PrintWriter out;

private boolean talking=true;

public ServerThread(Socket s,int c)

{ threadNum = c;

socket = s;

}

public void actionPerformed(ActionEvent e)

{ Object source = e.getSource();

try{

if(source==t.btnSend)

{ out.println(t.getTalk());

t.clearTalk();

}else

if(source==t.btnEnd)

{ out.println("谈话过程被对方终止");

out.close();

in.close();

talking = false;

}

}catch(IOException ex){

}

}

public void run()

{ try{

t=new talkServerFrm(new Integer(threadNum).toString(),this);

t.setSize(500,500);

t.show();

in = new BufferedReader(new

InputStreamReader(socket.getInputStream()));

out = new PrintWriter(socket.getOutputStream(),true);

}catch(Exception e){

}

new Thread()

{ public void run()

{ try{

while(true)

{ checkInput();

sleep(1000);

}

}catch (InterruptedException ex){

}catch(IOException ex){

}

}

}.start();

while(talking)

{ }

t.dispose();

}

private void checkInput() throws IOException

{ String line;

if((line=in.readLine())!=null)

t.setPartner(line); file://这是界面类里的方法,

file://用来将line的内容输出到用户界面

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你关于Java Socket网络编程的问题。 Java Socket网络编程Java语言中常用的网络通信技术之一,它可以实现客户端与服务端之间的通信,常用于开发网络应用程序、网络游戏等。下面我们来看一个基于Java Socket网络编程的应用实例。 假设我们有一个简单的聊天室应用程序,它由一个服务端和多个客户端组成。服务端负责接收客户端连接请求,并将客户端发送的消息广播给所有连接到服务端的客户端,客户端负责向服务端发送消息并接收来自服务端的消息。 1. 服务端实现 首先,我们需要编写服务端的代码。服务端需要监听指定的端口,等待客户端的连接请求,并在接收到连接请求时为每个客户端创建一个独立的线程进行处理。以下是服务端的代码示例: ```java import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.err.println("Could not listen on port: 4444."); System.exit(-1); } while (listening) { new ServerThread(serverSocket.accept()).start(); } serverSocket.close(); } } class ServerThread extends Thread { private Socket socket = null; public ServerThread(Socket socket) { super("ServerThread"); this.socket = socket; } public void run() { try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { out.println(inputLine); } out.close(); in.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 2. 客户端实现 接下来,我们需要编写客户端的代码。客户端需要连接到服务端指定的端口,并在连接成功后启动一个单独的线程用于接收来自服务端的消息。以下是客户端的代码示例: ```java import java.io.*; import java.net.*; public class Client implements Runnable { private static Socket socket = null; public static void main(String[] args) throws IOException { String hostName = "localhost"; int portNumber = 4444; try { socket = new Socket(hostName, portNumber); } catch (UnknownHostException e) { System.err.println("Don't know about host " + hostName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to " + hostName); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); new Thread(new Client()).start(); String userInput; while ((userInput = stdIn.readLine()) != null) { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println(userInput); if (userInput.equals("exit")) { break; } } socket.close(); } public void run() { try { BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Server: " + inputLine); } in.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上就是一个简单的基于Java Socket网络编程的聊天室应用程序的实现。在实际开发中,我们可以根据具体需求对代码进行修改和优化,使其更符合实际业务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值