1.服务器两台电脑链接。
第一个类
package com.company;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
/**
* Created by ttc on 2018/1/18.
*/
//客户端程序
public class TcpClient {
public static void main(String[] args) throws IOException {
//去连接服务器,指定(ip和端口)
Socket socket = new Socket("10.25.41.8",999);
//客户端发送hello给服务器
String str = "CNGSB";
// FileOutputStream fileOutputStream = new FileOutputStream("d:/helloworld.txt");
byte[] buffer = str.getBytes();
// fileOutputStream.write(buffer,0,buffer.length);
//
OutputStream os = socket.getOutputStream();
os.write(buffer,0,buffer.length);
System.out.println("退出");
}
}
第二个类
package com.company;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by ttc on 2018/1/18.
*/
public class Main {
public static void main(String[] args) throws IOException {
//监听端口 999
//服务器端需要创建2个Socket,其中一个socket是用来监听端口的(获得客户端连接的)
ServerSocket serverSocket = new ServerSocket(999);
System.out.println("开始监听");
Socket socket = serverSocket.accept();//等待接受连接,阻塞
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[100];
int len = inputStream.read(buffer);
//把读到的字节转换成字符串,显示出来
String string = new String(buffer,0,len);
System.out.println(string);
System.out.println("退出");
}
}
2.监听在从第一基础上在完善以下以IP地址来确定。
第一个类
package com.company;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
/**
* Created by ttc on 2018/1/19.
*/
public class ClientThread extends Thread {
Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
}
@Override//和某个客户端通信(收发数据)
public void run()
{
while (true)
{
try {
InputStream inputStream = null;
try {
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[100];
System.out.println(Thread.currentThread().getName()+"开始读取数据");
int len = 0;//,阻塞
try {
len = inputStream.read(buffer);
} catch (IOException e) {
System.out.println("客户端已经退出");
e.printStackTrace();
break;
}
//将信息发回给客户端
OutputStream os = null;
try {
os = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.write(buffer, 0, len);
} catch (IOException e) {
e.printStackTrace();
}
//把读到的字节转换成字符串,显示出来
String string = new String(buffer, 0, len);
System.out.println(string);
} catch (Exception exception) {
System.out.println("客户端已经退出dd");
break;
}
}
}
}
第二个类
package com.company;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
/**
* Created by ttc on 2018/1/18.
*/
//客户端程序
public class TcpClient {
public static void main(String[] args) throws IOException, InterruptedException {
//去连接服务器,指定(ip和端口)
Socket socket = new Socket("10.25.41.7",5555);
OutputStream os = null;
InputStream is = null;
//客户端发送hello给服务器
String str = "";
Scanner scanner = new Scanner(System.in);
// FileOutputStream fileOutputStream = new FileOutputStream("d:/helloworld.txt");
// for(int i = 0; i < 3; i++)
while(true)
{
System.out.println("请输入要和服务器说的话");
str = scanner.next();
if(str.equals("bye"))
{
break;
}
byte[] buffer = str.getBytes();
os = socket.getOutputStream();//获得输出流
os.write(buffer,0,buffer.length);//往输出流里写数据,相当于向服务器放送信息
is = socket.getInputStream();
byte[] bufferRead = new byte[100];
int len = is.read(bufferRead);
String string = new String(bufferRead,0,len);
System.out.println("服务器回应" + string);
}
if(os!=null)
{
os.close();
}
if(is!=null)
{
is.close();
}
socket.close();
System.out.println("退出");
}
}
第三个类
package com.company;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by ttc on 2018/1/18.
*/
public class TcpServer {
public static void main(String[] args) throws IOException {
//监听端口 999
//服务器端需要创建2个Socket,其中一个socket是用来监听端口的(获得客户端连接的)
ServerSocket serverSocket = new ServerSocket(5555);
System.out.println("开始监听");
while (true)
{
Socket socket = serverSocket.accept();//等待接受连接,阻塞
ClientThread clientThread = new ClientThread(socket);
clientThread.start();
}
// for(int i = 0; i < 3; i++) {
// InputStream inputStream = socket.getInputStream();
// byte[] buffer = new byte[100];
// System.out.println("开始读取数据");
// int len = inputStream.read(buffer);//,阻塞
//
// //将信息发回给客户端
// OutputStream os = socket.getOutputStream();
// os.write(buffer, 0, len);
//
// //把读到的字节转换成字符串,显示出来
// String string = new String(buffer, 0, len);
// System.out.println(string);
// }
}
}
3.ubc和ipc这两个必须要会的以上面的基础上来完善以改变(必须会)。
第一个类
package com.company;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* Created by ttc on 2018/1/19.
*/
public class SimpleUDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(6666);
while (true)
{
byte[] buffer = new byte[1000];
DatagramPacket datagramPacketRecv = new DatagramPacket(buffer,buffer.length);
System.out.println("准备接收数据");
datagramSocket.receive(datagramPacketRecv);//代码阻塞,等待客户端发送数据
//得到客户端发过来的数据
byte[] buffer2 = datagramPacketRecv.getData();
String string = new String(buffer2, 0, buffer2.length);
System.out.println(string);
InetAddress inetAddress = datagramPacketRecv.getAddress();//得到客户端的地址信息
String ipClinent = inetAddress.getHostAddress();
System.out.println(ipClinent);
int port = datagramPacketRecv.getPort();//获得客户端程序的端口
System.out.println("客户端发送端口" + port);
System.out.println("收到数据");
//给客户端回应信息
DatagramPacket datagramPacketSend = new DatagramPacket(buffer2,buffer2.length,inetAddress,port);
datagramSocket.send(datagramPacketSend);
}
}
}
第二个类
package com.company;
import java.io.IOException;
import java.net.*;
/**
* Created by ttc on 2018/1/19.
*/
public class SimpleUdpClient {
public static void main(String[] args) throws IOException {
String serverIp = "127.0.0.1";
int port = 6666;
DatagramSocket datagramSocket = new DatagramSocket();
InetAddress inetAddress = InetAddress.getByName(serverIp);
String string = "hello";
byte[] buffer = string.getBytes();
DatagramPacket datagramPacketSend = new DatagramPacket(buffer, buffer.length, inetAddress, port);
datagramSocket.send(datagramPacketSend);
byte[] bufferRecv = new byte[1000];
DatagramPacket datagramPacketRecv = new DatagramPacket(bufferRecv,bufferRecv.length);
datagramSocket.receive(datagramPacketRecv);//阻塞效果
byte[] data = datagramPacketRecv.getData();
String string1 = new String(data, 0 ,data.length);
System.out.println(string1);
}
}
- 服务器应用返回服务器应用在第三题以上改善一下
第一个类
package com.company;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
/**
* Created by ttc on 2018/1/18.
*/
//客户端程序
public class TcpClient {
public static void main(String[] args) throws IOException, InterruptedException {
//去连接服务器,指定(ip和端口)
Socket socket = new Socket("127.0.0.1",5555);
OutputStream os = null;
InputStream is = null;
//客户端发送hello给服务器
String str = "";
Scanner scanner = new Scanner(System.in);
// FileOutputStream fileOutputStream = new FileOutputStream("d:/helloworld.txt");
// for(int i = 0; i < 3; i++)
while(true)
{
System.out.println("请输入一个数");
str = scanner.next();
if(!str.equals("quit"))//那用户输入的就应该是一个数字形式的字符串
{
try{
Integer.parseInt(str);
byte[] buffer = str.getBytes();
os = socket.getOutputStream();//获得输出流
os.write(buffer,0,buffer.length);//往输出流里写数据,相当于向服务器放送信息
is = socket.getInputStream();
byte[] bufferRead = new byte[100];
int len = is.read(bufferRead);
String string = new String(bufferRead,0,len);
if(string.equals("0"))
{
System.out.println("您输入的是质数");
}
else if(string.equals("1"))
{
System.out.println("您输入的不是质数");
}
}
catch (Exception ex)
{
System.out.println("请输入正确的数字");
}
}
else{
break;
}
}
if(os!=null)
{
os.close();
}
if(is!=null)
{
is.close();
}
socket.close();
System.out.println("退出");
}
}
第二个类
package com.company;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by ttc on 2018/1/18.
*/
public class TcpServer {
public static void main(String[] args) throws IOException {
//监听端口 999
//服务器端需要创建2个Socket,其中一个socket是用来监听端口的(获得客户端连接的)
ServerSocket serverSocket = new ServerSocket(5555);
System.out.println("开始监听");
while (true)
{
Socket socket = serverSocket.accept();//等待接受连接,阻塞
ClientThread clientThread = new ClientThread(socket);
clientThread.start();
}
// for(int i = 0; i < 3; i++) {
// InputStream inputStream = socket.getInputStream();
// byte[] buffer = new byte[100];
// System.out.println("开始读取数据");
// int len = inputStream.read(buffer);//,阻塞
//
// //将信息发回给客户端
// OutputStream os = socket.getOutputStream();
// os.write(buffer, 0, len);
//
// //把读到的字节转换成字符串,显示出来
// String string = new String(buffer, 0, len);
// System.out.println(string);
// }
}
}
第三个类
package com.company;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
/**
* Created by ttc on 2018/1/19.
*/
public class ClientThread extends Thread {
Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
}
public boolean isZhiShu(int num)
{
for(int i = 2; i < num; i++)
{
if(num % i == 0)
{
return false;
}
}
return true;
}
@Override//和某个客户端通信(收发数据)
public void run()
{
while (true)
{
try {
InputStream inputStream = null;
try {
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[100];
System.out.println(Thread.currentThread().getName()+"开始读取数据");
int len = 0;//,阻塞
try {
len = inputStream.read(buffer);
} catch (IOException e) {
System.out.println("客户端已经退出");
e.printStackTrace();
break;
}
//把读到的字节转换成字符串,显示出来
String string = new String(buffer, 0, len);
System.out.println(string);
int num = Integer.parseInt(string);
String response = "";
boolean b = isZhiShu(num);
if(b==true)//是质数
{
response = "0";
}
else
{
response = "1";
}
byte[] bytes = response.getBytes();
//将信息发回给客户端
OutputStream os = null;
try {
os = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.write(bytes, 0, bytes.length);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception exception) {
System.out.println("客户端已经退出dd");
break;
}
}
}
}
``` 879
网友评论