用java中socket实现socket聊天
1, 什么是socket
Socket 是指网络套接字,什么是套接字呢? 这是网络上一种端对端的网络协议,端口就是进程号,socket在网络中让这两个端通信形成端口直接通信,所以socket的参数可想而知就是两端ip和端口号了;
再说在网络中,总要有人付出,要在网络中等着别人链接,不然的话你一点想连别人,别人在两点想连你,怎么也不可能连上,总有哟个人需要做等待的角色,这个角色就是服务端也就是serverSocket,他在网络中提供一个固定的ip和端口号套接字;
2, 在Java中实现socket接口
在Java中提供了两个用来建立socket链接都在java.net包中,在客户端要用Socket(addr,port)//参数是远程服务的地址和端口号;另一个在服务端中用ServerSocket(port) 建立一个等待的socket端,同时使用serversocket.accept() 在服务端动态等待并接受一个前来请求链接的socket请求
一旦serversocket.accept()方法获得一个链接,就会封装出一个Socket对象服务端可用这个对象,获得客户端的信息并向客户端发送信息;话不多说,上代码;
package testpackage;
/*
* @author:mayeye
* @about:服务端
* */
import java.net.*;
import java.io.*; public class TestServer {
public static void main(String[] args) {
//try {
System.out.println("=============================");
ServerSocket server=null;
try {
server=new ServerSocket(1233);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Socket client = null;
try {
client = server.accept();
System.out.println("ip为:"+client.getInetAddress()+"的客户端链接了");
System.out.println("port为:"+client.getPort()+"的客户端链接了");
System.out.println("Localport为:"+client.getLocalPort()+"的客户端链接了"); } catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line = null;
BufferedReader is = null;
try {
is = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter os = null;
try {
os = new PrintWriter(client.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Client:"+is.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
line=sin.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} while(!line.equals("bye")){
os.println(line);
os.flush();
System.out.println("Server:"+line);
try {
System.out.println("Client:"+is.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
line=sin.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
os.close();
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("********************************");
/*}catch(Exception e) {
System.out.println("-------------------------------");
}*/
}
}
上面是服务端代码
package testpackage;
import java.net.*;
import java.io.*;
public class testsocket {
public static void main(String[] args) {
try {
Socket client=new Socket("127.0.0.1",1233);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));//构建一个标准输入流
PrintWriter os=new PrintWriter(client.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(client.getInputStream()));//由client对象构造一个输入流
String readline;
System.out.println("ip为:"+client.getInetAddress()+"的客户端链接了");
readline=sin.readLine();
while(!readline.equals("bye")) {
os.println(readline);
os.flush();
System.out.println("Client:"+readline);
System.out.println("---*****************---");
System.out.println("Server:"+is.readLine());
readline=sin.readLine(); //从系统标准输入读入一字符串s
}
os.close();
is.close();
client.close(); } catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.print("====================找不到主机名");
} catch (IOException e) {
System.out.print("===================IO错误");
} }
}
上面是服务端代码;
大家都是成年人,所以目录结构就不用我说了吧
这只是用java实现了一个最简易的socket聊天,必须一问一答,而且没有界面;
预告一下,下一篇博客我将接介绍界面的解决方案;