게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
본삭금) tcp 소캣프로그래밍 좀 도와주세요
게시물ID : programmer_9296짧은주소 복사하기
작성자 : 스미르쪼
추천 : 0/5
조회수 : 468회
댓글수 : 5개
등록시간 : 2015/04/12 17:43:31
옵션
  • 베스트금지
  • 본인삭제금지
서버-클라이언트 역할을 나눠서 메세지를 주고 받아야 하는데요
문제는 제가 자바에 대해 아는게 없습니다. 이클립스도 어제 받아서 좀 건드려 봤네요. 
예제 코드는 있는데 이 코드의 각 부분이 무슨 역활을 하는지 모르겠습니다.  인터넷 뒤져봐도 소스코드는 있는데 설명은 없네요.
혹시 이 미련한 놈을 좀 도와주실 분이 계시면 아래 예제 코드 각 부분이 무슨 역할 하는지만 좀 알려주세요..

client

import java.io.*;
import java.net.*;
public class Client{
Socket requestSocket;
ObjectOutputStream out;
  ObjectInputStream in;
  String message;
Client(){}
void run()
{
try{
requestSocket = new Socket("localhost", 2004);
System.out.println("Connected to lo
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage("Hi my server");
message = "bye";
sendMessage(message);
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Client client = new Client();
client.run();
}
}


server


import java.io.*;
import java.net.*;
public class Server{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Server(){}
void run()
{
try{
providerSocket = new ServerSocket(2004, 10);
System.out.println("Server is waiting for connection from client");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("bye"))
sendMessage("bye");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Server server = new Server();
while(true){
server.run();
}
}
}

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호