62,614
社区成员
发帖
与我相关
我的任务
分享
private void socket(){
ServerSocket ss;
try {
ss = new ServerSocket(9988);
//等待连接 客户端
System.out.println("正在侦听");
Socket s=ss.accept();
System.out.println("-----------连接成功-----------");
InputStream in=s.getInputStream();
out= s.getOutputStream();
//读取从客户端法发来的信息
while(true) {
//读取从客户端发来的信息
byte[] b=new byte[1024];
in.read(b);
String info=new String(b).trim();
//在文本栏里显示
area_showWindow.append("客户端:"+info+"\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void btn_send(){
btn_send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String info = area_inputWindow.getText();
if(info.equals("1")){
try {
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
area_showWindow.append("客户端: "+info+"\r\n");
try {
out.write(info.getBytes());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
area_inputWindow.setText("");
}
});
}
* @return the total number of bytes read into the buffer, or
* <code>-1</code> if there is no more data because the end of
* the stream has been reached.
* @exception IOException If the first byte cannot be read for any reason
* other than the end of the file, if the input stream has been closed, or
* if some other I/O error occurs.
* @exception NullPointerException if <code>b</code> is <code>null</code>.
* @see java.io.InputStream#read(byte[], int, int)
*/
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
if there is no more data because the end of stream has been reached return -1.
未读取返回-1判断下。import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketBasic {
public static final int PORT = 23123;
public static final int BUFFER_SIZE = 4096;
public static void main(String[] args) throws Exception {
new Server().start();
new Client().start();
}
////////////////////////////////////////////////////////////////
/**
* 服务器端
*/
static class Server {
public void start() throws Exception {
// 服务器端使用单独的侦听线程,该线程不会停止,所以程序需要手动停止
Runnable runnable = new Runnable() {
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
while (true) {
// 接受客户端连接,并且将连接得到的 Socket 对象交给会话线程处理
Socket accept = serverSocket.accept();
new Thread(new ServerSession(accept)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
new Thread(runnable).start();
}
}
/**
* 会话线程
*/
static class ServerSession implements Runnable {
private Socket socket;
ServerSession(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
run0();
} catch (Exception e) {
e.printStackTrace();
}
}
private void run0() throws Exception {
InputStream $i = this.socket.getInputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int readCount;
// read() 方法返回 -1 表示客户端关闭了连接
while ((readCount = $i.read(buffer)) != -1) {
System.out.println("收到消息: " + new String(buffer, 0, readCount));
}
System.out.println("客户端关闭了连接,会话结束。");
}
}
/**
* 客户端
*/
static class Client {
public void start() throws Exception {
for (int i = 0; i < 3; i++) {
Socket socket = new Socket("127.0.0.1", PORT);
String msg = "Hello, now is " + System.currentTimeMillis();
socket.getOutputStream().write(msg.getBytes());
socket.close();
Thread.sleep(1000);
}
}
}
}