Java 网络编程之 TCP 实现简单的聊天系统
客户端
1、连接服务器Socket
2、发送消息
packagelesson02;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.net.InetAddress;
importjava.net.Socket;
/**
*客户端
*/
publicclassTcpClientDemo1{
publicstaticvoidmain(String[]args){
Socketsocket=null;
OutputStreamos=null;
try{
//1、要知道服务器的地址端口号
InetAddressserverIP=InetAddress.getByName("127.0.0.1");
intport=9999;
//2、创建一个socket连接
socket=newSocket(serverIP,port);
//3、发送消息IO流
os=socket.getOutputStream();
os.write("你好,欢迎学习狂神学Java".getBytes());
}catch(Exceptione){
e.printStackTrace();
}finally{
if(os!=null){
try{
os.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(socket!=null){
try{
socket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
服务端
1、建立服务的端口ServerSocket
2、等待用户的连接accept
3、接收用户的消息
packagelesson02;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.ServerSocket;
importjava.net.Socket;
/**
*服务端
*/
publicclassTcpServerDemo1{
publicstaticvoidmain(String[]args){
ServerSocketserverSocket=null;
Socketsocket=null;
InputStreamis=null;
ByteArrayOutputStreambaos=null;
try{
//1、我得有一个地址
serverSocket=newServerSocket(9999);
while(true){
//2、等待客户端连接过来
socket=serverSocket.accept();
//3、读取客户端的消息
is=socket.getInputStream();
//管道流
baos=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen;
while((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
}
/*
byte[]buffer=newbyte[1024];
intlen;
while((len=is.read(buffer))!=-1){
Stringmsg=newString(buffer,0,len);
System.out.println(msg);
}
*/
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭资源
if(baos!=null){
try{
baos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(is!=null){
try{
is.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(socket!=null){
try{
socket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(serverSocket!=null){
try{
serverSocket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
服务端
1、建立服务的端口ServerSocket
2、等待用户的连接accept
3、接收用户的消息
packagelesson02;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.ServerSocket;
importjava.net.Socket;
/**
*服务端
*/
publicclassTcpServerDemo1{
publicstaticvoidmain(String[]args){
ServerSocketserverSocket=null;
Socketsocket=null;
InputStreamis=null;
ByteArrayOutputStreambaos=null;
try{
//1、我得有一个地址
serverSocket=newServerSocket(9999);
while(true){
//2、等待客户端连接过来
socket=serverSocket.accept();
//3、读取客户端的消息
is=socket.getInputStream();
//管道流
baos=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen;
while((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
}
/*
byte[]buffer=newbyte[1024];
intlen;
while((len=is.read(buffer))!=-1){
Stringmsg=newString(buffer,0,len);
System.out.println(msg);
}
*/
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭资源
if(baos!=null){
try{
baos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(is!=null){
try{
is.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(socket!=null){
try{
socket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(serverSocket!=null){
try{
serverSocket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
以上就是Java网络编程之TCP实现简单的聊天系统的详细内容,更多关于Java实现简单的聊天系统的资料请关注毛票票其它相关文章!