[완료]java에서 socket write는 손실이 생길수 없나요?

anaud2의 이미지

안녕하세요

자바를 하다 막히는 부분이 너무 많아서 염치 불구하고 계속 질문을 드리겠습니다.

java에서 write는 손실이 생길수 없는가요?

여기 아래를 보면 read_data로 메세지를 전송받다가 끊어질때를 대비하는걸 만들었는데요
write에서는 생기지 않는가 하는게 궁금합니다.
public static byte[] read_data(InputStream in, int len) throws Exception
{
// if(len < 1) return new byte[0];
int bcount = 0, n = 0, read_retry_count = 0;
byte buf[] = new byte[len];
while(bcount < len) {
n = in.read(buf, bcount, len - bcount);
if(n > 0) bcount += n;
else if(n == -1) throw new IOException("inputstream has returned an unexpected EOF");
else if(n == 0 && ++read_retry_count == 10) throw new IOException("inputstream-read-retry-count( 10) exceed !");
}
return buf;
}
//////////////////////////////////////////
//소켓 전송부분의 간략한 내용
client = new Socket(ipAddress, port);
bos = new BufferedOutputStream(client.getOutputStream());
bos.write(send);

이런식으로 전송을하게 되는데요 api를 찾아봐도 BufferedOutputStream 클래스의
write는 리턴값이 없습니다.
없는 이유는 전송중 손실이 안되어서 인건지 아님 다른 이유에서인지 알수가 없네요
다른 이유에서라면 전송중 손실을 막는 함수를 구현해야하는데 얼마나 전송한지를 리턴하지
않으니 좀 막막하네요
답변 부탁드립니다.

wishforyou의 이미지

대부분의 자바 API는 에러 처리 위해 리턴값 보다는 예외(Exception)를 이용합니다.
언급하신 write() 메소드를 API 문서에서 찾아보면 다음과 같이 정의되어 있습니다.
public void write(byte[] b) throws IOException
이 메소드는 I/O 에러가 발생했을때 IOException 예외를 발생시는데 이 예외를 적절히 처리하시면 됩니다.

client = new Socket(ipAddress, port);
bos = new BufferedOutputStream(client.getOutputStream());
try {
bos.write(send);
} catch (IOException ex) {
// 예외 처리
}

그리고 넘겨준 byte배열이 얼마나 전송됐는지는 - OutputStream을 직접 구현하지 않는 이상 - 일반적으로 알아낼 방법이 없습니다.

자바에서 예외(Exception) 처리는 매우 중요한 부분입니다. 개념과 쓰임을 꼭 익히시기 바랍니다.

개인적으로 자바 API 문서는 일관성 있게 매우 잘 만들어졌다고 생각합니다.
C의 레퍼런스 가이드처럼 꼭 옆에 끼고 꼼꼼히 살피면 클래스나 메소드의 쓰임에 큰 어려움은 없을 겁니다.

anaud2의 이미지

답변 감사합니다.
예외 처리를 꼭 익혀야 겠네요 도움 감사합니다.(_ _)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.