public class Client implements Runnable{
private static Socket s;
private Socket getInstance(){
if(s==null){
try {
return s=new Socket("127.0.0.1",8000);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return s;
}
public void sendMsg() {
while(true){
try {
run();
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void run() {
System.out.println("come into run");
try {
Socket c = getInstance();
OutputStream ops =c.getOutputStream();
ops.write(new byte[]{0000});
ops.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
public static void main(String[] args) {
Client c = new Client();
c.sendMsg();
}
}
server code
public class Server {
private ServerSocket serverSocket;
public Server(){
try {
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(8000));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("服务启动");
}
public void service() {
while (true) {
Socket s = null;
try {
s = serverSocket.accept();
System.out.println("come a new request~ "+s.getInetAddress()+":"+s.getPort());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(s!=null)
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Server server = new Server();
server.service();
}
}
My idea is to service every 3 seconds sends a data packet , the current situation will throw an exception:
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0 (Native Method)
at java.net.SocketOutputStream.socketWrite (Unknown Source)
at java.net.SocketOutputStream.write (Unknown Source)
at com.ls.test.Client.run (Client.java: 44)
at com.ls.test.Client.sendMsg (Client.java: 28)
at com.ls.test.Client.main (Client.java: 57)
I found that may be server -side socket close to the reason so removed the shutdown code :
finally {
if (s! = null)
try {
s.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
But now the server does only accept one request , do not know why
------ Solution ------------------------------------ --------
serverSocket.accept();//这一行会被阻塞
look accept the introduction, note the red part :
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made .
It says this method before the arrival of the new connection will always be blocked.
while you write the client approach:
private Socket getInstance(){}
This method guarantees only to establish a connection .
That client connection is established for the first time no new connections , while the services side of the loop as it receives the first connection and print immediately after went innocently waiting for the second connected , it is certainly not wait , So ......
------ For reference only --------------------------- ------------
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client implements Runnable
{
private static Socket s;
private Socket getInstance()
{
if (s == null)
{
try
{
return s = new Socket("127.0.0.1", 8000);
}
catch (IOException e)
{
e.printStackTrace();
}
}
return s;
}
@Override
public void run()
{
while (true)
{
try
{
System.out.println("come into run");
Socket c = getInstance();
OutputStream ops = c.getOutputStream();
ops.write(new byte[]
{
0000
});
ops.flush();
Thread.sleep(3000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
Client c = new Client();
new Thread(c).start();
}
}
------ For reference only -------------------- -------------------
I used your code in the service clients receive only one request , do not know why
------ For reference only --------------- ------------------------
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server implements Runnable
{
private ServerSocket serverSocket;
private Socket s;
public Server()
{
try
{
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(8000));
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("服务启动");
}
public void run()
{
while (true)
{
try
{
if(null == s)
s = serverSocket.accept();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
String str = null;
while(null != (str = dis.readUTF()))
{
System.out.println("come a new request~ " + s.getInetAddress()
+ ":" + s.getPort() + str);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
Server server = new Server();
new Thread(server).start();
}
}
------ For reference only -------- -------------------------------
carefully measured for a moment :
service side :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Server implements Runnable
{
private ServerSocket serverSocket;
private Socket s;
public Server()
{
try
{
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(8000));
s = serverSocket.accept();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("服务启动");
}
public void run()
{
while (true)
{
try
{
InputStream is = s.getInputStream();
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String str = null;
while(null != (str = buf.readLine()))
{
System.out.println("come a new request~ " + s.getInetAddress()
+ ":" + s.getPort() + str + new SimpleDateFormat("mm:ss").format(new Date()));
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
Server server = new Server();
new Thread(server).start();
}
}
Client:
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Client implements Runnable
{
private static Socket s;
private Socket getInstance()
{
if (s == null)
{
System.out.println("nullllllllllll");
try
{
return s = new Socket("127.0.0.1", 8000);
}
catch (IOException e)
{
e.printStackTrace();
}
}
return s;
}
public void run()
{
while (true)
{
try
{
String d = new SimpleDateFormat("mm:ss").format(new Date()) + System.getProperty("line.separator");
System.out.println("come into run" + d);
Socket c = getInstance();
OutputStream ops = c.getOutputStream();
ops.write(d.getBytes());
ops.flush();
Thread.sleep(3000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
Client c = new Client();
new Thread(c).start();
}
}
Results:
------ For reference only ----- ----------------------------------
this ok. Thank you very much ~
I compared the code between us and found that the difference in the reception on your server side code is:
InputStream is = s.getInputStream();
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String str = null;
while(null != (str = buf.readLine()))
{
System.out.println("come a new request~ " + s.getInetAddress()
+ ":" + s.getPort() + str + new SimpleDateFormat("mm:ss").format(new Date()));
}
And I was like this:
s = serverSocket.accept();
System.out.println("come a new request~ "+s.getInetAddress()+":"+s.getPort());
Did I write this? Has been thought through
------ For reference only ---------------------------------- -----
Yes, my client needs only requires the establishment of a socket connection. Therefore, the use of single-case model , but the service side where my problem lies in the way ? Written almost feel and why you can keep listening to the news, I can only accept one
------ For reference only ------------------- --------------------
Yes, my client needs only requires the establishment of a socket connection. Therefore, the use of single-case model , but the service side where my problem lies in the way ? Written almost feel and why you can keep listening to the news, I can only accept one
Please landlord to consider carefully , and then read my reply on the 6th floor . I think it has been said enough detailed.
------ For reference only -------------------------------------- -
I understand, thank you , I wrote while listening again, and each request will be repeated listens , has been blocked. tks
没有评论:
发表评论