Java网络编程——TCP实例

1、客户端

  1.1:创建服务端点

  1.2:获取已有数据

  1.3:通过socket输出流将数据发送给服务端

  1.4:读取服务端反馈信息

  1.5:关闭socket

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException; public class TCPClient
{ static Socket socket;
public static void main(String[] args)
{
BufferedReader br = null;
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
try
{
socket = new Socket("10.9.194.71", 8638);
bi = new BufferedInputStream(socket.getInputStream());
bo = new BufferedOutputStream(socket.getOutputStream()); br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null)
{
bo.write(line.getBytes());
bo.flush();
if (line.equals("quit"))
{
bi.close();
bo.close();
br.close();
socket.close();
break;
}
byte[] buf = new byte[1024];
int len = bi.read(buf); System.out.println(new String(buf, 0, len));
} } catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (bi != null)
bi.close();
if (br != null)
br.close();
if (bo != null)
bo.close();
if (socket != null)
socket.close();
} catch (IOException e)
{ e.printStackTrace();
}
}
} }

2、服务器

import java.io.IOException;
import java.net.ServerSocket; public class TCPServer
{
static int acount = 0; public static void main(String[] args)
{ try
{
ServerSocket serverSocket = new ServerSocket(8638);
while (true)
new Thread(new MultiClient(serverSocket.accept(), ++acount))
.start();
} catch (IOException e)
{
e.printStackTrace();
}
}
} import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Date; public class MultiClient implements Runnable
{ private Socket client;
private int acount;
private BufferedInputStream bi = null;
private BufferedOutputStream bo = null; public MultiClient(Socket client, int acount)
{ this.client = client;
this.acount = acount;
} @Override
public void run()
{
try
{
Date start = new Date(); String recordStart = start + " :: "
+ client.getInetAddress().getHostAddress() + " :: 第"
+ acount + "用户成功连接";
System.out.println(recordStart);
WriteLog.writeLog(recordStart);
bi = new BufferedInputStream(client.getInputStream());
BufferedOutputStream bo = new BufferedOutputStream(
client.getOutputStream()); while (true)
{
byte[] buf = new byte[1024];
int len = bi.read(buf);
String info = new String(buf, 0, len);
System.out.println(info);
bo.write(("Has Receive:" + info).getBytes());
bo.flush();
if (info.equals("quit"))
break;
}
Date end = new Date(); String recordEnd = end + " :: "
+ client.getInetAddress().getHostAddress() + " :: 第"
+ acount + "用户退出连接";
System.out.println(recordEnd);
WriteLog.writeLog(recordEnd); } catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (bi != null)
bi.close();
if (bo != null)
bo.close();
if (client != null)
client.close();
} catch (IOException e)
{ e.printStackTrace();
}
}
}
} import java.io.File;
import java.io.FileWriter; public class WriteLog
{
public static void writeLog(String str)
{
File logFile = new File("server-log.txt");
try
{
FileWriter out = new FileWriter(logFile, true);
out.append(str + '\n');
out.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
上一篇:Java网络编程(TCP协议-服务端和客户端交互)


下一篇:Oracle常用的性能诊断语句