服务端:
package com.kkoolerter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPOne2OneServer {
/**
* 一对一的基于TCP的服务器
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ServerSocket server = null;
Socket socket = null;
DataInputStream dis = null;
PrintStream out = null;
try {
// 设置侦听端口
server = new ServerSocket(12345);
// 获取Socket
socket = server.accept();
// 获取输入流
dis = new DataInputStream(socket.getInputStream());
// 获取输出流
out = new PrintStream(socket.getOutputStream());
// 获取客户端的IP地址
InetAddress addr = server.getInetAddress();
//发送欢迎信息
out.println("Welcome...");
byte buffer [] = new byte[1024];
//判断用户是否已经退出
int len = dis.read(buffer);
String msg = new String(buffer,len);
while(!msg.equals("quit")){
System.out.println("from client:"+new String(msg));
msg = dis.readLine();
}
System.out.println("client leaved");
} catch (Exception e) {
e.printStackTrace();
}finally{
dis.close();
out.close();
socket.close();
server.close();
System.exit(0);
}
}
* 一对一的基于TCP的服务器
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ServerSocket server = null;
Socket socket = null;
DataInputStream dis = null;
PrintStream out = null;
try {
// 设置侦听端口
server = new ServerSocket(12345);
// 获取Socket
socket = server.accept();
// 获取输入流
dis = new DataInputStream(socket.getInputStream());
// 获取输出流
out = new PrintStream(socket.getOutputStream());
// 获取客户端的IP地址
InetAddress addr = server.getInetAddress();
//发送欢迎信息
out.println("Welcome...");
byte buffer [] = new byte[1024];
//判断用户是否已经退出
int len = dis.read(buffer);
String msg = new String(buffer,len);
while(!msg.equals("quit")){
System.out.println("from client:"+new String(msg));
msg = dis.readLine();
}
System.out.println("client leaved");
} catch (Exception e) {
e.printStackTrace();
}finally{
dis.close();
out.close();
socket.close();
server.close();
System.exit(0);
}
}
}
客户端:
package com.kkoolerter;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.text.Format;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.text.Format;
import javax.sound.sampled.AudioFormat.Encoding;
public class TCPOne2OneClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Socket socket = null;
PrintStream ps = null;
DataInputStream dis = null;
* @param args
*/
public static void main(String[] args) throws Exception {
Socket socket = null;
PrintStream ps = null;
DataInputStream dis = null;
try {
socket = new Socket("127.0.0.1", 12345);
ps = new PrintStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
String msg = dis.readLine();
System.out.println("from server:" + msg);
byte buffer[] = new byte[1024];
System.in.read(buffer);
msg = new String(buffer, 0);
System.out.println(msg);
while (!msg.equals("quit")) {
ps.println(msg);
System.in.read(buffer);
msg = new String(buffer, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ps.close();
dis.close();
socket.close();
socket = new Socket("127.0.0.1", 12345);
ps = new PrintStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
String msg = dis.readLine();
System.out.println("from server:" + msg);
byte buffer[] = new byte[1024];
System.in.read(buffer);
msg = new String(buffer, 0);
System.out.println(msg);
while (!msg.equals("quit")) {
ps.println(msg);
System.in.read(buffer);
msg = new String(buffer, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ps.close();
dis.close();
socket.close();
}
}
}
}