欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

浅析java网络编程

程序员文章站 2024-02-01 16:30:40
...

         Socket简称套接字,用于实现网络上客户和服务器之间的连接,也就是说网络上两个或两个以上通信的进程间总有一个连接,这个连接的端点称为套接字,套接字是比较低层次上的通信,不同操作系统对Scoket有不同的支持方式,不同的开发工具,对Socket的实现也均不相同,而且Scoket在TCP与UDP两大协议族系中有各自的工作方式。

 

           网络中互相通信的两端,其中提供服务的一端叫服务器,而请求服务的一端叫客户机。Scoket通常用来实现Client/Server设计过程,所谓Client/Server是指通信双方一方作为服务器(Server)等待客户(Client)提出请求并予以响应。客户则在需要服务时向服务器提出申请。服务器监听网络端口,一旦有客户请求,就会启动一个服务进程来响应客户,同时继续监听服务端口,使其他客户的请求也能及时得到处理。一般连接过程是:Server端监听某个端口(port)是否有连接请求,Client端向Server端发出连接(Connect)请求,Server端向Client端发出接受(Accept)消息,一个连接就建立起来了。Server端和Client端都可以通过Send,Write等方法与对方通信。

        在java中实现Scoket通讯:

        首先在服务器端创建ServerSocket类对象,来创建服务器对象,其格式如下:

        ServerSocket 服务器对象名 = new ServerSocket(端口号);

通过调用accept()方法来创建一个Scoket对象,其格式如下:

        Scoket 对象名 = 服务器对象名.accept();

服务器端可以用利用这个Scoket对象与客户端进行通讯,通过Scoket对象拿到输入输出流。在所有通信结束后,关闭输入输出流,再关闭Scoket。

 

        服务器和客户端属于两个不同项目

        以下是服务器类代码的实现:(MyServer类和ServerThread类在同一个项目包中)

 

 

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

 

/**

 * 服务器类

 *

 */

public class MyServer {

//队列保存对象

public static ArrayList<ServerThread> list  = new ArrayList<ServerThread>();

 

//Map定义用户名密码集合

public static Map<String,String> userInfoMap = new HashMap<String,String>();

 

 

 

public static void main(String[] args) {

//初始化用户登录信息

for(int i=0;i<5;i++){

userInfoMap.put("user"+i, "pwd"+i);

}

MyServer ms = new MyServer();

ms.initServer(9090);

}

public void initServer(int port){

boolean flag = true;

try {

//创建服务器套接字对象,并给定端口号

ServerSocket server = new ServerSocket(port);

System.out.println("服务器创建成功,端口号为"+port+",等待客户机连接");

 

while(flag){

//等待客户机连接服务器

Socket socket = server.accept();

System.out.println("客户机连接成功!");

//创建服务器线程对象

ServerThread st = new ServerThread(socket);

list.add(st);

st.start();

}

server.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

 

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.Socket;

 

import com.huaxin.client.CommonNet;

/**

 * 服务器线程对象

 */

public class ServerThread extends Thread{

public Socket socket;

public InputStream ins;

public OutputStream ous;

public ServerThread(Socket socket){

this.socket = socket;

}

public void run() {

try{

//拿到输入输出流

ins= socket.getInputStream();

ous =socket.getOutputStream();

 

//把字节流封装为字符流

InputStreamReader isReader = new InputStreamReader(ins);

BufferedReader read = new BufferedReader(isReader);

 

//发送消息给客户机

String msg = "欢迎连接服务器!\r\n";

CommonNet.sendMsg(ous,msg);

 

String user_msg = "请输入用户名:\r\n";

CommonNet.sendMsg(ous,user_msg);

//接收客户端发送过来的用户名

String username = read.readLine();

String user_pwd = "请输入密码:\r\n";

CommonNet.sendMsg(ous,user_pwd);

//接收客户端发送过来的用户名

String password = read.readLine();

 

//验证是否登录成功

if(MyServer.userInfoMap.containsKey(username)){

String pwd = MyServer.userInfoMap.get(username);

if(pwd.equals(password)){

CommonNet.sendMsg(ous,"true\r\n");

//循环读取消息

String value = "";

while(!"bye".equals(value)){

value = CommonNet.readMsg(ins);

System.out.println("客户机说:"+value);

//把读取到的消息转发到其他客户

CommonNet.castMsg(this,username+"说:"+value);

}

}else{

CommonNet.sendMsg(ous,"false\r\n");

}

}else{

CommonNet.sendMsg(ous,"false\r\n");

}

 

//关闭相关链接

ous.flush();

ous.close();

ins.close();

socket.close();

}catch(Exception e){

e.printStackTrace();

}

}

 

}

 

 

         下面是客户端类代码的实现:(MyClient类和CommonNet类属于同一项目的包中)

 

 

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.Socket;

import java.util.Scanner;

 

public class MyClient {

public static void main(String[] args) {

//连接服务器

try {

Socket client = new Socket("localhost",5656);

InputStream ins = client.getInputStream();

OutputStream ous = client.getOutputStream();

 

//把字节流封装为字符流

InputStreamReader isReader = new InputStreamReader(ins);

BufferedReader read = new BufferedReader(isReader);

//读取欢迎

String value = read.readLine();

System.out.println(value);

//读取请输入用户名

String value1 = read.readLine();

System.out.println(value1);

//发送消息

Scanner scan = new Scanner(System.in);

String scan_value = scan.nextLine();

CommonNet.sendMsg(ous, scan_value+"\r\n");

 

value1 = read.readLine();

System.out.println(value1);

scan_value = scan.nextLine();

CommonNet.sendMsg(ous, scan_value+"\r\n");

 

if("true".equals(read.readLine())){

System.out.println("登录成功");

}else{

System.out.println("登录失败");

}

 

ins.close();

ous.close();

client.close();

} catch (Exception e) {

e.printStackTrace();

}

}

 

}

 

 

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}

 

import java.io.InputStream;

import java.io.OutputStream;

import com.huaxin.server.MyServer;

import com.huaxin.server.ServerThread;

 

public class CommonNet {

/**

* @param ous

* @param msg

* @throws Exception

*/

public static void sendMsg(OutputStream ous,String msg) throws Exception{

byte[] bytes = msg.getBytes();

ous.write(bytes);

}

/**

* @param ins

* @return

* @throws Exception

*/

public static String readMsg(InputStream ins) throws Exception{

//接收客户机发送的消息

int readByte = ins.read();

String value = "";

StringBuffer sb = new StringBuffer();

//循环读取字节

while(readByte!=13){

sb.append((char)readByte);

readByte = ins.read();

}

//String.trim()去除字符串前后空格回车

value = sb.toString().trim();

return value;

}

/**

* @param st

* @param value

* @throws Exception

*/

public static void castMsg(ServerThread st,String value) throws Exception{

for(int i=0;i<MyServer.list.size();i++){

OutputStream list_ous = MyServer.list.get(i).ous;

if(MyServer.list.get(i)!=st){

sendMsg(list_ous,value+"\r\n");

}

}

}

}