ftp.nice.ch/pub/next/connectivity/conferences/IRC.0.5J.s.tar.gz#/IRC/TCPConnection.m

This is TCPConnection.m in view mode; [Download] [Up]

/*
 *	IRC.app -- IRC client program for NEXTSTEP
 *	Copyright (C)1995 Norihiro Itoh
 *
 *	このソースはIRC.appの一部です。
 *	This file is part of IRC.app.
 *
 *	本プログラムはフリー・ソフトウェアです。あなたは、Free Software 
 *	Foundationが公表したGNU一般公有使用許諾の「バージョン2」或いは
 *	それ以降の各バージョンの中からいずれかを選択し、そのバージョンが
 *	定める条項に従って本プログラムを再頒布または変更することができま
 *	す。
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	本プログラムは有用とは思いますが、頒布にあたっては、市場性及び特
 *	定目的適合性についての暗黙の保証を含めて、いかなる保証も行ないま
 *	せん。詳細についてはGNU一般公有使用許諾書をお読みください。
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	あなたは、本プログラムと一緒にGNU一般公有使用許諾の写しを受け取っ
 *	ているはずです。そうでない場合は、Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USAへ手紙を書いてください。
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *	作者のメールアドレス: nito@scorpio.bekkoame.or.jp
 *	Author's e-mail address: nito@scorpio.bekkoame.or.jp
 */	

#import "TCPConnection.h"

@implementation TCPConnection

- init
{
    [super init];
    sock = -1;
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
	return nil;
    }
    return self;
}

- (int)connectTo:(char *)hostname by:(u_short)port
{
    struct hostent		*hp;
    struct sockaddr_in		sin;

    if ((hp = gethostbyname(hostname)) == NULL) {
	return TCP_UNKNOWN_HOST;
    }


    bzero((char*)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);

    if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
	return TCP_CONNECT_FAILED;
    }
    return TCP_CONNECTED;
}

- (int)sendData:(char *)buffer sizeOf:(int)length
{
    int		nleft, nwritten;

    nwritten = 0;
    nleft = length;
    while (nleft > 0) {
	nwritten = write(sock, buffer, nleft);
	if (nwritten <= 0) return nwritten;	/* write error */
	nleft -= nwritten;
	buffer += nwritten;
    }
    return (nwritten - nleft);
}

- (int)receiveData:(char *)buffer sizeOf:(int)length
{
    int		nleft, nread;

    nleft = length;
    while (nleft > 0) {
	nread = read(sock, buffer, nleft);
	if (nread < 0) return nread;	/* read error */
	else if (nread == 0) break;
	nleft -= nread;
	buffer += nread;
    }
    return (length - nleft);
}

- (int)receiveLine:(char *)buffer
{
    int		n, rc;
    char	c;

    for (n = 1; n < 1025; n++) {
	if ((rc = read(sock, &c, 1)) == 1) {
	    *buffer++ = c;
	    if (c == '\n') break;
	} else if (rc == 0) {
	    if (n == 1) return 0;
	    else break;
	} else	return -1;
    }
    *buffer = 0;
    return n;
}

-(int)getFD
{
    return sock;
}

- shutDown
{
    shutdown(sock, 2);
    return self;
}

- close
{
    close(sock);
    return self;
}

- shutDownAndClose
{
    shutdown(sock, 2);
    close(sock);
    return self;
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.