java客户端应用程序_Java客户端/服务器应用程序与套接字?

服务器侦听连接。当客户端建立连接时。客户端可以发送数据。在当前的示例中,客户端发送消息“Hi my server”。要终止连接,客户端发送消息“再见”。然后服务器也会发送消息“再见”。最后连接结束,服务器等待其他连接。这两个程序应该在同一台机器上运行。但是,如果要在两台机器上运行它们,可以通过运行服务器的机器的IP地址来更改地址“localhost”。

服务器

import java.io.*;

import java.net.*;

public class Provider{

ServerSocket providerSocket;

Socket connection = null;

ObjectOutputStream out;

ObjectInputStream in;

String message;

Provider(){}

void run()

{

try{

//1. creating a server socket

providerSocket = new ServerSocket(2004, 10);

//2. Wait for connection

System.out.println("Waiting for connection");

connection = providerSocket.accept();

System.out.println("Connection received from " + connection.getInetAddress().getHostName());

//3. get Input and Output streams

out = new ObjectOutputStream(connection.getOutputStream());

out.flush();

in = new ObjectInputStream(connection.getInputStream());

sendMessage("Connection successful");

//4. The two parts communicate via the input and output streams

do{

try{

message = (String)in.readObject();

System.out.println("client>" + message);

if (message.equals("bye"))

sendMessage("bye");

}

catch(ClassNotFoundException classnot){

System.err.println("Data received in unknown format");

}

}while(!message.equals("bye"));

}

catch(IOException ioException){

ioException.printStackTrace();

}

finally{

//4: Closing connection

try{

in.close();

out.close();

providerSocket.close();

}

catch(IOException ioException){

ioException.printStackTrace();

}

}

}

void sendMessage(String msg)

{

try{

out.writeObject(msg);

out.flush();

System.out.println("server>" + msg);

}

catch(IOException ioException){

ioException.printStackTrace();

}

}

public static void main(String args[])

{

Provider server = new Provider();

while(true){

server.run();

}

}

}

客户端

import java.io.*;

import java.net.*;

public class Requester{

Socket requestSocket;

ObjectOutputStream out;

ObjectInputStream in;

String message;

Requester(){}

void run()

{

try{

//1. creating a socket to connect to the server

requestSocket = new Socket("localhost", 2004);

System.out.println("Connected to localhost in port 2004");

//2. get Input and Output streams

out = new ObjectOutputStream(requestSocket.getOutputStream());

out.flush();

in = new ObjectInputStream(requestSocket.getInputStream());

//3: Communicating with the server

do{

try{

message = (String)in.readObject();

System.out.println("server>" + message);

sendMessage("Hi my server");

message = "bye";

sendMessage(message);

}

catch(ClassNotFoundException classNot){

System.err.println("data received in unknown format");

}

}while(!message.equals("bye"));

}

catch(UnknownHostException unknownHost){

System.err.println("You are trying to connect to an unknown host!");

}

catch(IOException ioException){

ioException.printStackTrace();

}

finally{

//4: Closing connection

try{

in.close();

out.close();

requestSocket.close();

}

catch(IOException ioException){

ioException.printStackTrace();

}

}

}

void sendMessage(String msg)

{

try{

out.writeObject(msg);

out.flush();

System.out.println("client>" + msg);

}

catch(IOException ioException){

ioException.printStackTrace();

}

}

public static void main(String args[])

{

Requester client = new Requester();

client.run();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值