'java.net.SocketException: Broken pipe (Please give me design advice.)
public class NewsServer {
Socket socket;
ArticleData ad;
DataType dt;
boolean isRecv = false;
DataOutputStream dos;
private static final int THREAD_CNT = 5;
private static ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_CNT, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
});
private static ExecutorService pollThreadPool = Executors.newSingleThreadExecutor();
public NewsServer() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(7777);
while (true) {
socket = serverSocket.accept();
try {
threadPool.execute(new WriteThread(socket));
pollThreadPool.execute(new PollingThread(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (IOException e) {
try {
serverSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
// polling
class PollingThread implements Runnable {
private Socket tsocket;
public PollingThread(Socket socket) {
this.tsocket = socket;
}
@Override
public void run() {
try {
DataOutputStream dos = new DataOutputStream(tsocket.getOutputStream());
ArticleData ads = new ArticleData(dos);
while (!tsocket.isClosed()) {
ads.sendPolling();
Thread.sleep(Constant.POLLING_INTERVAL);
}
} catch (SocketException e) {
try {
tsocket.close();
} catch (IOException ie) {
ie.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
tsocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// send news
class WriteThread implements Runnable {
private Socket tsocket = null;
private boolean readySend = true;
private boolean doSend = true;
public WriteThread(Socket socket) {
this.tsocket = socket;
}
@Override
public void run() {
int dataTypeDiv = 0;
try {
DataOutputStream dos = new DataOutputStream(tsocket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(tsocket.getInputStream()));
NewsDataFile newsDataFile = null;
StringBuffer bodyBuffer = new StringBuffer();
while (true) {
if (doSend) {
newsDataFile = sendNewsData(dos);
doSend = false;
}
try {
int readByteCount = br.read();
if (readByteCount == DataType.STX) {
doSend = false;
} else if (readByteCount == DataType.ETX) {
if (dataTypeDiv == DataType.ACK) {
doSend = true;
if (newsDataFile != null) {
newsDataFile.removeNewsData();
}
} else if (dataTypeDiv == DataType.NAK) {
doSend = true;
}
} else if (readByteCount == DataType.ACK) {
dataTypeDiv = readByteCount;
} else if (readByteCount == DataType.NAK) {
dataTypeDiv = readByteCount;
} else {
bodyBuffer.append(readByteCount);
doSend = false;
}
} catch (IOException e) {
readySend = true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
tsocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param dos
* @throws Exception
* @throws ParseException
* @throws IOException
*/
private NewsDataFile sendNewsData(DataOutputStream dos) throws Exception, ParseException, IOException {
String jsonNewsData = null;
NewsDataFile newsDataFile = new NewsDataFile();
while (true) {
jsonNewsData = newsDataFile.getJsonNewsData();
if (jsonNewsData != null) {
JSONParser jsonParse = new JSONParser();
JSONObject articleObject = (JSONObject) jsonParse.parse(jsonNewsData);
String status = (String) articleObject.get("status");
String sn = (String) articleObject.get("sn");
String code = (String) articleObject.get("code");
String wdate = (String) articleObject.get("wdate");
String sdate = (String) articleObject.get("sdate");
String cate = (String) articleObject.get("cate");
String title = (String) articleObject.get("title");
String reporter = (String) articleObject.get("reporter");
String stockcd1 = (String) articleObject.get("stockcd1");
String stockcd2 = (String) articleObject.get("stockcd2");
String stockcd3 = (String) articleObject.get("stockcd3");
String stockcd4 = (String) articleObject.get("stockcd4");
String stockcd5 = (String) articleObject.get("stockcd5");
String stockcd6 = (String) articleObject.get("stockcd6");
String stockcd7 = (String) articleObject.get("stockcd7");
String stockcd8 = (String) articleObject.get("stockcd8");
String prepare1 = (String) articleObject.get("prepare1");
String prepare2 = (String) articleObject.get("prepare2");
String body = (String) articleObject.get("body");
ArticleData articleData = new ArticleData(dos);
articleData.setArticle(status, sn, code, wdate, sdate, cate, title, reporter, stockcd1, stockcd2,
stockcd3, stockcd4, stockcd5, stockcd6, stockcd7, stockcd8, prepare1, prepare2, body);
articleData.writeDataExternal();
dos.flush();
articleData = null;
articleObject = null;
jsonParse = null;
break;
}
}
return newsDataFile;
}
}
public static final byte[] getbytes(byte src[], int offset, int length) {
byte dest[] = new byte[length];
System.arraycopy(src, offset, dest, 0, length);
return dest;
}
public static void main(String[] args) {
new NewsServer();
}
}
- When a client connects, the news server sends polling every 30 seconds to maintain the connection.
- When a news file is created, the news content is converted into byte and transmitted to the waiting client.
I implemented it as described above, but the connection is frequently lost. If you have any design problems or have any advice, please let me know. I'm embarrassed about the source of the server socket.
java.net.SocketException: Broken pipe (Write failed)
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:134)
at java.io.DataOutputStream.write(DataOutputStream.java:88)
at com.ns.data.ArticleData.sendPolling(ArticleData.java:272)
at com.ns.NewsServer$PollingThread.run(JNewsServer.java:89)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:750)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|