import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TcpIpServer4 {
public static void main(String args[]) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(7777);
System.out.println("서버가 준비되었습니다.");
} catch(IOException e) {
e.printStackTrace();
}
while(true) {
Socket socket = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
socket = serverSocket.accept();
System.out.println("[" + socket.getInetAddress()+ "] 로부터 연결요청이 들어왔습니다.");
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int dansu = Integer.parseInt(br.readLine().replaceAll("단", ""));
// 구구단 로직 기술
String result = "";
for(int i=1 ; i<=9 ; i++) {
result += dansu + " X " + i + " = " + (dansu*i) + "\n";
}
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(result);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bw != null) try { bw.close(); } catch(IOException e) {}
if(br != null) try { br.close(); } catch(IOException e) {}
if(socket != null) try { socket.close(); } catch(IOException e) {}
}
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpIpClient4 {
public static void main(String args[]) {
Socket socket = null;
BufferedReader br = null;
BufferedWriter bw = null;
if(args.length != 1) {
System.out.println("입력된 문자열의 길이가 틀립니다");
}
try {
socket = new Socket("127.0.0.1", 7777);
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(args[0] + "\n");
bw.flush();
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
}
System.out.println("연결이 종료되었습니다.");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bw != null) try { bw.close(); } catch(IOException e) {}
if(br != null) try { br.close(); } catch(IOException e) {}
if(socket != null) try { socket.close(); } catch(IOException e) {}
}
}
}
'Study > Programming' 카테고리의 다른 글
JSP 구구단 (0) | 2009.05.27 |
---|---|
자바 TCP MultiChat (0) | 2009.05.26 |
자바 Network Chat (0) | 2009.05.26 |
자바 Swing 달력 스케줄 관리 (0) | 2009.05.25 |
자바 데이터 변경 (0) | 2009.05.25 |