近来开始系统研究Java,陆续贴出一些研究成果代码。
今天要说的是关于那个socket服务器与客户端程序。
服务器端使用的是ServerSocket,通过创建ServerSocket来绑定服务器端口,服务器端创建一个进程监听这个端口,当有客户端连接后,则接收客户端发送的消息,并打印在控制台。
客户端则是通过连接服务器的指定端口来获得socket,并通过socket写入信息,每次可以输入一行字符,如果输入了exit,则退出程序。
服务器端是用的ServerTest类,代码如下:
package org.flc.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest implements Runnable {
private ServerSocket ss;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServerTest(){
try {
ss = new ServerSocket(10000);
} catch (IOException e) {
System.err.println("Create Socket server error");
System.err.println("May be the port 10000 is used by other program");
}
}
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Thread sleep interrupted");
}
try {
if(ss == null){
return;
}
socket = ss.accept();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Accept socket error");
}
if(socket != null){
String RemoteIP = socket.getInetAddress().getHostAddress();
String RemotePort = ":"+socket.getLocalPort();
System.out.println("A client come in!IP:"+RemoteIP+RemotePort);
InputStream ins = null;
try {
ins = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Get socket inputstream error");
}
if(ins != null){
in = new BufferedReader(new InputStreamReader(ins));
String line = null;
try {
line = in.readLine();
if(line != null)
System.out.println("Client send is :" + line);
else
System.out.println("Client closed");
} catch (IOException e) {
System.out.println("Read inputstream error");
}
try {
OutputStream outs = socket.getOutputStream();
out = new PrintWriter(outs,true);
out.println("Your Message Received!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Get socket outputstream error");
}
try {
if(null != out)
out.close();
if(null != in)
in.close();
if(null != socket)
socket.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Close inputstream of socket error");
}
}
}
}
}
public static void main(String [] args){
ServerTest server = new ServerTest();
Thread thread = new Thread(server);
thread.start();
}
}
客户端类名称为ClientTest,代码如下:
package org.flc.util;
import java.io.*;
import java.net.*;
/**
*
标题: Socket通信客户端程序
*
作者:
*
时间: 1:09:07 PM
* @版本 1.0 Apr 12, 2007
*/
public class ClientTest
{
Socket socket;
BufferedReader in;
PrintWriter out;
public ClientTest() {
}
public boolean connect(String ipAddress, int port){
System.out.println("Try to Connect to "+ipAddress+":"+port);
try {
socket = new Socket(ipAddress,port);
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Connect to server "+ipAddress+":"+port+" failed");
return false;
}
System.out.println("The Server Connected!");
System.out.println("Please enter some Character:");
BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
try {
out = new PrintWriter(socket.getOutputStream(),true);
String msg = line.readLine();
if(msg.equals("exit")){
out.close();
return false;
}
out.println(msg);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(out != null)
out.close();
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Close inputstream error");
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Close socket error");
}
}
}
return true;
}
public static void main(String[] args)
{
ClientTest client = new ClientTest();
while(client.connect("127.0.0.1", 10000));
}
};