1,java的服务器通过serverSocket建立TCP服务器,利用 Socket 建立TCP 客户端。
2,cmd 调出 dos 命令窗口 ,输入ipconfig /all 来查看机器上所有ip 地址
3,创建(服务器)和(客户端)的代码如下:
package cn.day1;
/*
*服务器
*
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server{
public static void main(String[] args) throws IOException {
//创建监听socked
ServerSocket server=new ServerSocket(50000);
System.out.println("服务端");
//创建通讯socket
Socket socket = server.accept();
//用输入流接收客户端请求
getInputStream stream= socket.getInputStream();
int date;
byte[] by=new byte[1024];
while((date=stream.read(by))!=-1){
String temp=new String(by,0,date);
System.out.println(temp);
}
stream.close();
}
}
package cn.day1;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 客户端
*/
public class sock {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket so=new Socket("192.168.15.117",50000);
//用输出流输出客户端请求
OutputStream stream = so.getOutputStream();
String name="娜娜";
stream.write(name.getBytes());
stream.close();
}
}
4,要想多个客户端给一个服务器发信息,那么我们用到(线程)以及(死循环)。
代码如下:
package cn.day1;
/*
* 建立线程
*/
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class Tread extends Thread{
Socket socket=null;
@Override
public void run() {
while(true){
InputStream stream = null;
try {
stream = socket.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bytes=new byte[1024];
int dex;
try {
while((dex=stream.read(bytes))!=-1){
//数据在二进制数组里头,转换成字符串类型
String temp=new String(bytes,0,dex);
System.out.println(temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package cn.day1;
/*
* 使用无限循环接收 多个客户端 请求
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class X {
public static void main(String[] args) throws IOException {
//z创建监听socked
ServerSocket server=new ServerSocket(50000);
System.out.println("服务端");
//创建通讯socket
while(true){
Socket socket = server.accept();
Tread t1=new Tread();
t1.socket=socket;
t1.start();
}
}
}
5,服务器接收客户端请求以后反馈给客户端的代码如下:
package cn.day1;
/*
*服务器
*
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
//创建监听socked
ServerSocket server=new ServerSocket(50000);
System.out.println("服务端");
//创建通讯socket
Socket socket = server.accept();
//用输入流接收客户端请求
InputStream stream= socket.getInputStream();
int date;
byte[] by=new byte[1024];
while((date=stream.read(by))!=-1){
String temp=new String(by,0,date);
System.out.println(temp);
}
socket.shutdownInput();
//服务器给客户端一个相应
//打开输出流,发送数据
OutputStream os = socket.getOutputStream();
String name="我是服务器,客户端登录成功";
os.write(name.getBytes());
os.close();
stream.close();
}
}
package cn.day1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 客户端
*/
public class Socket {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket so=new Socket("192.168.15.117",50000);
//用输出流输出客户端请求
OutputStream stream = so.getOutputStream();
String name="娜娜";
stream.write(name.getBytes());
so.shutdownOutput();
//服务器接收客户端反馈
InputStream is = so.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
name="";
while((name=br.readLine())!=null){
System.out.println(name);
}
is.close();
stream.close();
}
}
注意:用完一次服务器或客户端我们该关闭它们。
列如:
so.shutdownOutput();