C# 소켓 예제에서 발생한 에러가 무슨 뜻인지 모르겠습니다.
글쓴이: gomnutu / 작성시간: 금, 2012/07/06 - 12:14오전
Client 쪽 소스 입니다.
using System; using System.Net; using System.Net.Sockets; using System.Text; namespace client { class ClientClass { public static Socket socket; public static byte[] getbyte = new byte[1024]; public static byte[] setbyte = new byte[1024]; public const int sPort = 8999; [STAThread] static void Main(string[] args) { string sendstring = null; string getstring = null; IPAddress serverIP = IPAddress.Parse("127.0.0.1"); IPEndPoint serverEndPoint = new IPEndPoint(serverIP, sPort); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine("-------------------------------------------------"); Console.WriteLine(" 서버로 접속을 시작합니다.[엔터를 입력하세요]"); Console.WriteLine("-------------------------------------------------"); Console.ReadLine(); try { socket.Connect(serverEndPoint); if (socket.Connected) { Console.WriteLine(">> 정상적으로 연결 되었습니다.(전송한 데이터를 입력해주세요)"); } while (true) { Console.Write(">>"); sendstring = Console.ReadLine(); if (sendstring != String.Empty) { int getValueLength = 0; setbyte = Encoding.UTF7.GetBytes(sendstring); socket.Send(setbyte, 0, setbyte.Length, SocketFlags.None); Console.WriteLine("송신데이터 : {0}| 길이 : {1}", sendstring, setbyte.Length); socket.Receive(getbyte, 0, getbyte.Length, SocketFlags.None); getValueLength = byteArrayDefrag(getbyte); getstring = Encoding.UTF7.GetString(getbyte, 0, getValueLength + 1); Console.WriteLine(">> 수신된 데이터 : {0}| 길이{1}", getstring, getValueLength + 1); } getbyte = new byte[1024]; } } catch (System.ArgumentNullException socketEx) { Console.WriteLine("[Error]:{0}", socketEx.Message); } catch (System.Exception commonEx) { Console.WriteLine("[Error]:{0}", commonEx.Message); } } public static int byteArrayDefrag(byte[] sData) { int endLength = 0; for (int i = 0; i < sData.Length; i++) { if((byte)sData[i] != (byte)0) { endLength = i; } } return endLength; } } } /* static class Program { /// <summary> /// 해당 응용 프로그램의 주 진입점입니다. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } */
Server 쪽은 잘 돌아가면서 Client가 접속할때까지 기다리는데
Client를 실행하면
[Error]:문자열 참조가 문자열의 인스턴스로 설정되지 않았습니다.
매개 변수 이름: s
'System.ArgumentNullException' 형식의 첫째 예외가 mscorlib.dll에서 발생했습니다.
0x167c 스레드가 종료되었습니다(코드: 0 (0x0)).
0x498 스레드가 종료되었습니다(코드: 0 (0x0)).
'[7452] client.vshost.exe: 관리' 프로그램이 종료되었습니다(코드: 0 (0x0)).
무슨 뜻인지 이해가 안갑니다. 어떤 에러죠?
Forums:
댓글 달기