Browse Source

tcp example

tobby48 5 years ago
parent
commit
739cb1a5f3
1 changed files with 11 additions and 8 deletions
  1. 11
    8
      src/kr/co/swh/lecture/network/tcp/TcpClient.java

+ 11
- 8
src/kr/co/swh/lecture/network/tcp/TcpClient.java View File

@@ -1,5 +1,7 @@
1 1
 package kr.co.swh.lecture.network.tcp; 
2 2
 
3
+import java.io.BufferedWriter;
4
+import java.io.OutputStreamWriter;
3 5
 import java.io.PrintWriter;
4 6
 import java.net.InetAddress;
5 7
 import java.net.Socket;
@@ -18,18 +20,19 @@ import java.net.Socket;
18 20
  */
19 21
 public class TcpClient {
20 22
 	public static void main(String[] args) {
21
-		InetAddress ia = null;		// 접속할 서버의 주소를 저장할 변수
22 23
 		Socket sock = null;		// 서버에 접속할 소켓 변수
23
-		PrintWriter out = null;		// 테이터를 전송할  Write 변수
24 24
 		try{
25
-			// 서버 주소생성
26
-			ia = InetAddress.getByName("127.0.0.1");
27 25
 			// 서버 연결
28
-			sock = new Socket(ia, 9999);
26
+			sock = new Socket("127.0.0.1", 9999);
29 27
 			// 서버에 메세지 전송
30
-			out = new PrintWriter(sock.getOutputStream());
31
-			out.println("SWHAcademy\n");
32
-			out.flush();
28
+//			PrintWriter out = new PrintWriter(sock.getOutputStream()); // 테이터를 전송할  Write 변수
29
+//			out.println("SWHAcademy\n");
30
+//			out.flush();
31
+			
32
+			BufferedWriter w = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
33
+			w.write("SWHAcademy\n");
34
+			w.close();
35
+			
33 36
 			// 접속 끊기
34 37
 			sock.close();
35 38
 		}catch(Exception e){