我的第一个Java Socket交互程序:
服务器端代码:
客户端代码:
服务器端代码:
package scoket.server;
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
public class Action {
/**
* @param args
*/
public static void main(String[] args) {
try {
//实例化服务器Socket对象
ServerSocket server = new ServerSocket(150, 10);
//监听获取客户端
Socket client = server.accept();
//接受到客户端连接后打印客户端地址
System.out.println("连接来自:"+client.getRemoteSocketAddress().toString());
//获取客户端输入流
InputStream in = client.getInputStream();
ObjectInputStream reader = new ObjectInputStream(in);
//读取并打印客户端发送的信息
String s = reader.readUTF();
System.out.println("客户端 :" + s);
//服务器向客户端反馈信息
//获得输出流
ObjectOutputStream writer = new ObjectOutputStream(client.getOutputStream());
//向客户端反馈信息
writer.writeUTF("已经收到你的信息");
writer.flush();
//两秒后再次响客户端发送信息
Thread.sleep(2000);
writer.writeUTF("再次确认");
writer.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
public class Action {
/**
* @param args
*/
public static void main(String[] args) {
try {
//实例化服务器Socket对象
ServerSocket server = new ServerSocket(150, 10);
//监听获取客户端
Socket client = server.accept();
//接受到客户端连接后打印客户端地址
System.out.println("连接来自:"+client.getRemoteSocketAddress().toString());
//获取客户端输入流
InputStream in = client.getInputStream();
ObjectInputStream reader = new ObjectInputStream(in);
//读取并打印客户端发送的信息
String s = reader.readUTF();
System.out.println("客户端 :" + s);
//服务器向客户端反馈信息
//获得输出流
ObjectOutputStream writer = new ObjectOutputStream(client.getOutputStream());
//向客户端反馈信息
writer.writeUTF("已经收到你的信息");
writer.flush();
//两秒后再次响客户端发送信息
Thread.sleep(2000);
writer.writeUTF("再次确认");
writer.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端代码:
package socket.client;
import java.io.*;
import java.net.*;
public class Action {
/**
* @param args
*/
public static void main(String[] args) {
try {
//设置需要请求的服务器地址
InetAddress ia = InetAddress.getByName("192.168.0.87");
//服务器向服务器发送请求
Socket client = new Socket(ia,150);
//获取输出流
OutputStream out = client.getOutputStream();
ObjectOutputStream writer = new ObjectOutputStream(out);
//向服务器发送信息
writer.writeUTF("我是客户端!!");
writer.flush();
//等待并接受服务器的两次信息反馈
ObjectInputStream reader = new ObjectInputStream(client.getInputStream());
System.out.println("服务器:"+reader.readUTF());
System.out.println("服务器:"+reader.readUTF());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class Action {
/**
* @param args
*/
public static void main(String[] args) {
try {
//设置需要请求的服务器地址
InetAddress ia = InetAddress.getByName("192.168.0.87");
//服务器向服务器发送请求
Socket client = new Socket(ia,150);
//获取输出流
OutputStream out = client.getOutputStream();
ObjectOutputStream writer = new ObjectOutputStream(out);
//向服务器发送信息
writer.writeUTF("我是客户端!!");
writer.flush();
//等待并接受服务器的两次信息反馈
ObjectInputStream reader = new ObjectInputStream(client.getInputStream());
System.out.println("服务器:"+reader.readUTF());
System.out.println("服务器:"+reader.readUTF());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}