展开全部
这个简单 我发个程序 你看下
package server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import util.StringUtil;
public class FromOnLineBankToBackSocket {
private static ServerSocket server; //定义服务器端口,用于客户短访问
private Socket client; // 定义服务器端套接字,用于为来访用户分配可使用的端口
private ClientThreads thread1; //定义线程对象
/*static {
try {
server = new ServerSocket(3000);
System.out.println(server.getLocalSocketAddress());
} catch (IOException e) {
}//实例服务器端口号
}*/
public FromOnLineBankToBackSocket() {
try {
server = new ServerSocket(9001);
System.out.println(server.getLocalSocketAddress());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
try {
client = server.accept(); //为来访用户分配可以使用的端口
System.out.println("***********"
+ client.getRemoteSocketAddress() + "已经连接"
+ "***********");
} catch (IOException e) {
e.printStackTrace();
}
if (client != null) { //如果端口号存在,则启动线程
thread1 = new ClientThreads(client);
thread1.start(); //运行线程
}
}
}
public static void main(String[] args) {
new FromOnLineBankToBackSocket();//启动服务端监听
}
class ClientThreads extends Thread { // 线程类
private InputStream is;
private OutputStream os;
private Socket client;
public ClientThreads(Socket client) {
this.client = client;
try {
os = client.getOutputStream();
is = client.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 接收客户端传来的数据
*
* @return
*/
public String receive() {
byte[] buf = new byte[1024];
String s = "";
try {
int count = 0;
int times = 0;
while (is.available() == 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
times++;
if (times == 10) {
break;
}
}
while (is.available() > 0) {
if ((count = is.read(buf)) != -1) {
s += new String(buf, 0, count, "UTF-8");
}
}
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
/**
* 向客户端发送响应代码
*
* @param s
*/
public boolean send(String msg) {
String serverIp = "192.168.0.41";
int portNo = 9578;
OutputStream out;
System.out.println("发送到后台的报文为:");
try {
Socket socket = new Socket(serverIp, portNo);
System.out.println("socket connected successful!");
out = (socket.getOutputStream());
out.write(msg.getBytes("UTF-8"));
out.flush();
try {
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 关闭响应的资源
*/
public void close() {
try {
is.close();
os.close();
client.close();
is = null;
os = null;
client = null;
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() { // 线程run方法
String msg = receive();
send(msg);
}
}
}
package server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import util.StringUtil;
public class FromOnLineBankToBackSocket {
private static ServerSocket server; //定义服务器端口,用于客户短访问
private Socket client; // 定义服务器端套接字,用于为来访用户分配可使用的端口
private ClientThreads thread1; //定义线程对象
/*static {
try {
server = new ServerSocket(3000);
System.out.println(server.getLocalSocketAddress());
} catch (IOException e) {
}//实例服务器端口号
}*/
public FromOnLineBankToBackSocket() {
try {
server = new ServerSocket(9001);
System.out.println(server.getLocalSocketAddress());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
try {
client = server.accept(); //为来访用户分配可以使用的端口
System.out.println("***********"
+ client.getRemoteSocketAddress() + "已经连接"
+ "***********");
} catch (IOException e) {
e.printStackTrace();
}
if (client != null) { //如果端口号存在,则启动线程
thread1 = new ClientThreads(client);
thread1.start(); //运行线程
}
}
}
public static void main(String[] args) {
new FromOnLineBankToBackSocket();//启动服务端监听
}
class ClientThreads extends Thread { // 线程类
private InputStream is;
private OutputStream os;
private Socket client;
public ClientThreads(Socket client) {
this.client = client;
try {
os = client.getOutputStream();
is = client.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 接收客户端传来的数据
*
* @return
*/
public String receive() {
byte[] buf = new byte[1024];
String s = "";
try {
int count = 0;
int times = 0;
while (is.available() == 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
times++;
if (times == 10) {
break;
}
}
while (is.available() > 0) {
if ((count = is.read(buf)) != -1) {
s += new String(buf, 0, count, "UTF-8");
}
}
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
/**
* 向客户端发送响应代码
*
* @param s
*/
public boolean send(String msg) {
String serverIp = "192.168.0.41";
int portNo = 9578;
OutputStream out;
System.out.println("发送到后台的报文为:");
try {
Socket socket = new Socket(serverIp, portNo);
System.out.println("socket connected successful!");
out = (socket.getOutputStream());
out.write(msg.getBytes("UTF-8"));
out.flush();
try {
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 关闭响应的资源
*/
public void close() {
try {
is.close();
os.close();
client.close();
is = null;
os = null;
client = null;
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() { // 线程run方法
String msg = receive();
send(msg);
}
}
}
展开全部
//服务器端程序
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;
public class MyServer {
public static void main(String[] args)
{
//给服务器开放一个端口
try {
ServerSocket ss = new ServerSocket(9000);
System.out.println("等待连接...");
Socket socket = ss.accept();//等待客户端连接
System.out.println("已连接");
//服务器向客户端发送数据
//得到输出流
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("hello,welcome to cs world!");
//得到网络输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/客户端程序
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.*;
public class MyClient {
public static void main(String[] args)
{
try {
//客户端向服务器发送一个连接请求
Socket socket = new Socket("127.0.0.1",9000);
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
//向服务器发送数据
//得到网络输出流
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("go go go");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;
public class MyServer {
public static void main(String[] args)
{
//给服务器开放一个端口
try {
ServerSocket ss = new ServerSocket(9000);
System.out.println("等待连接...");
Socket socket = ss.accept();//等待客户端连接
System.out.println("已连接");
//服务器向客户端发送数据
//得到输出流
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("hello,welcome to cs world!");
//得到网络输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/客户端程序
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.*;
public class MyClient {
public static void main(String[] args)
{
try {
//客户端向服务器发送一个连接请求
Socket socket = new Socket("127.0.0.1",9000);
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
//向服务器发送数据
//得到网络输出流
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("go go go");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
就在一个服务器类里面创建一个Socket对象,和一个ServerSocket对象就可以啦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询