프로그래밍과 잡담

[Qt] QTcpSocket, QTcpServer 본문

프로그래밍/Qt

[Qt] QTcpSocket, QTcpServer

크레온 2009. 8. 16. 20:12
QTcpSocket 중에서

readyRead()라는 시그널이 있을것이다. 이것의 경우는

클라이언트나 서버에서 데이터를 write를 했을시에 일어나는 시그널인데..

이것을 이용해서 여러가지를 이용할 수 있다.

connect(tcp,SIGNAL(readyRead()),this,SLOT(readProcess());

이런식으로 말이다.

connectToHost()라는 메소드가 있다.

위 메소드는 서버에 접속할 시에 사용한다.. 메소드 이름을 보면 알 수 있는 메소드이다.

void connectToHost ( const QString & hostName, quint16 port, OpenMode openMode = ReadWrite );

사용법은 간단하다.

connectToHost("호스트 네임이나 IP를 적으면된다", 포트를 쓴다. , 이 부분은 안써도 된다.);

state()라는것도 있는데..

현재 상태를 알아보는 것이다.


ConstantValueDescription
QAbstractSocket::UnconnectedState 0 The socket is not connected. 소켓이 연결되지 않은 상태
QAbstractSocket::HostLookupState 1 The socket is performing a host name lookup.
소켓이  호스트 이름을 룩업한 상태
QAbstractSocket::ConnectingState 2 The socket has started establishing a connection.
소켓이 연결중인 상태다.
QAbstractSocket::ConnectedState 3 A connection is established.
연결된상태
QAbstractSocket::BoundState 4 The socket is bound to an address and port (for servers).
주소와 포트를 바운드한 상태다
QAbstractSocket::ClosingState 5
The socket is about to close (data may still be waiting to be written). 닫는 중인 상태
QAbstractSocket::ListeningState 6
For internal use only.
연결을 기다리는 상태다.

위에 있는것들은 클라쪽에서 구현하는건데.. 상태알아보고 싶을때 사용하면 될듯..

사용법이야 if를 쓰던 switch를 쓰던..


뭐 나머지는 http://doc.trolltech.com/4.5/qabstractsocket.html#signals 여기 들어가서 참조를 하면되겠다.


QTcpServer


위에 처럼 여러가지 메소드가 있다.

내가 써 놓을껀..

nextPendingConnection() 하고 listen () 하고 위에 없지만 newConnection() 정도다.

그 외는 별로 할게 없어 보이고..

newConnection()는 이름 처럼 새로운 접속이 올 경우 발생하는 시그널이다.

사용법은 connect(this,SIGNAL(newConnection()), SLOT(makeConnection());

이런식으로 사용한다.


nextPendingConnection() 
Returns the next pending connection as a connected QTcpSocket object.
메소드는 위에 있는 설명 처럼 QTcpSoket* 반환 한다.

사용법은 newConnection()이 발생했을 시에 대응되는 슬롯에서 사용한다.

void ClassName::makeConnection()
{
     QTcpSocket * tcp = nextPendingConnection();
 .................
..................

}

위에 처럼 사용한다.

listen()은  클라이언트에서 연결을 할 수 있게 하는 메소드다..

이거 안하면 안된다..

listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 );

위에서  해줄꺼는 포트정도만 해주면 된다.

listen(5050);  이런식으로 사용한다.












반응형
Comments